This repository has been archived by the owner on Nov 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgenerate-package.mjs
190 lines (166 loc) · 3.87 KB
/
generate-package.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import inquirer from "inquirer";
import fs from "fs";
import { join } from "path";
import { exec } from "child_process";
const DEFAULT_TSUP_CONFIG = `import { defineConfig } from "tsup";
export default defineConfig({
entry: ["src/index.ts"],
sourcemap: true,
clean: true,
splitting: true,
dts: true,
outDir: "dist",
format: "esm",
outExtension() {
return {
js: ".js",
};
},
});
`;
const DEFAULT_SWC_CONFIG = `{
"$schema": "https://json.schemastore.org/swcrc",
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": false,
"decorators": false,
"dynamicImport": true
},
"transform": null,
"target": "es2017",
"loose": true,
"externalHelpers": false
},
"minify": false
}
`;
const DEFAULT_PACKAGE_JSON = {
name: "name",
version: "0.0.1",
description: "",
main: "dist/index.js",
types: "dist/index.d.ts",
type: "module",
scripts: {
test: 'echo "Error: no test specified" && exit 1',
build: "tsup",
dev: "tsup --watch",
},
keywords: [],
author: "",
license: "ISC",
devDependencies: {
"@ultrapkg/types": "workspace:*",
"@types/node": "^18.11.13",
tsup: "6.5.0",
},
dependencies: {},
bundledDependencies: ["@ultrapkg/types"],
};
const DEFAULT_TSCONFIG = {
extends: "../base.json",
include: ["src/**/*.ts"],
exclude: ["node_modules", "dist", "dist/types"],
};
const dirs = {
src: {
"index.ts": `export const name = "name";`,
},
"tsup.config.ts": DEFAULT_TSUP_CONFIG,
"swc.config.json": DEFAULT_SWC_CONFIG,
"package.json": DEFAULT_PACKAGE_JSON,
"tsconfig.json": DEFAULT_TSCONFIG,
};
const questions = [
{
type: "input",
name: "name",
message: "What is the name of the package?",
},
{
type: "confirm",
name: "scope",
message: "Is this package scoped?",
default: true,
},
{
type: "input",
name: "folder",
message: "What is the folder name?",
default: (answers) => answers.name,
},
{
type: "list",
name: "where",
message: "Where is the package?",
choices: ["packages", "apps"],
default: "packages",
},
{
type: "list",
name: "type",
message: "What type of package is it?",
choices: ["swc", "tsup", "none"],
default: "tsup",
},
];
async function main() {
const answers = await inquirer.prompt(questions);
let name = answers.name;
const simpleName = answers.name;
if (answers.scope) {
name = `@ultrapkg/${answers.name}`;
}
const { folder, where, type } = answers;
const path = `${where}/${folder}`;
if (!fs.existsSync(join(path, "src"))) {
console.log(`Creating ${path}`);
fs.mkdirSync(join(path, "src"), { recursive: true });
}
const packageJson = {
...DEFAULT_PACKAGE_JSON,
name,
};
console.log(`Creating package.json`);
// Create package.json
fs.writeFileSync(
`${path}/package.json`,
JSON.stringify(packageJson, null, 2)
);
// Create tsup.config.ts
if (type === "tsup") {
console.log(`Creating tsup.config.ts`);
fs.writeFileSync(`${path}/tsup.config.ts`, DEFAULT_TSUP_CONFIG);
}
// Create tsconfig.json
console.log(`Creating tsconfig.json`);
fs.writeFileSync(
`${path}/tsconfig.json`,
JSON.stringify(DEFAULT_TSCONFIG, null, 2)
);
// Create .swcrc
if (type === "swc") {
console.log(`Creating .swcrc`);
fs.writeFileSync(`${path}/.swcrc`, DEFAULT_SWC_CONFIG);
}
console.log(`Generating starter files...`);
// Create src/name.ts
fs.writeFileSync(
`${path}/src/${simpleName}.ts`,
`export const name = "${simpleName}";`
);
// Create src/index.ts
fs.writeFileSync(
`${path}/src/index.ts`,
`export { name } from "./${simpleName}";`
);
console.log(`Done!`);
console.log(`Installing dependencies...`);
exec("pnpm i", {
cwd: process.cwd() + `/${where}/${folder}`,
stdio: "inherit",
});
console.log(`Done!`);
}
main();