forked from netease-lcap/CodeWaveSummerCompetition2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.js
222 lines (198 loc) · 5.43 KB
/
create.js
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
const fsp = require('fs/promises');
const { resolve } = require('path');
const child_process = require('child_process');
const { promisify } = require('node:util');
const prettier = require('prettier');
const main = async (pkgName) => {
const pwd = process.cwd();
const meta = await getProjectMeta();
// const packageName =
const context = {
pwd,
fullName: `@${meta.name}/${pkgName}`,
packageName: pkgName,
meta,
path: resolve(pwd, 'packages', pkgName),
};
await createPackageWithLerna(context);
await adjustPackageJSON(context);
await appendConfigs(context);
await appendStyle(context);
await adjustPlayground(context);
};
async function getProjectMeta() {
const { name, description, keywords, license } = JSON.parse(
await fsp.readFile('./package.json'),
);
return { name, description, keywords, license };
}
async function createPackageWithLerna({ fullName }) {
return promisify(child_process.exec)(`yarn lerna create ${fullName} -y`);
}
async function adjustPackageJSON({ path, meta }) {
try {
const packagePath = resolve(path, 'package.json');
const pkg = JSON.parse(await fsp.readFile(packagePath));
delete pkg.type;
pkg.main = 'dist/index.umd.js';
pkg.module = 'dist/index.mjs';
pkg.source = 'src/index.ts';
pkg.types = 'dist/src/index.d.ts';
pkg.files = ['dist', 'style'];
pkg.description = meta.description;
pkg.keywords = meta.keywords;
pkg.license = meta.license;
pkg.peerDependencies = {
react: '>=18',
'react-dom': '>=18',
};
pkg.scripts = {
'build:css': 'yarn postcss notailwind.css -o dist/style/notailwind.css',
'build:type': 'tsc --emitDeclarationOnly',
'build:ts': 'vite build',
build: 'yarn build:ts && yarn build:type && yarn build:css',
prepublish: 'rm dist/tsconfig.tsbuildinfo',
};
const tmp = JSON.stringify(pkg);
await fsp.writeFile(
packagePath,
prettier.format(tmp, {
parser: 'json',
trailingComma: 'all',
printWidth: 80,
}),
{
encoding: 'utf-8',
},
);
} catch (e) {
console.log(e);
}
}
async function appendConfigs(context) {
const { path } = context;
await fsp.writeFile(resolve(path, 'jest.config.js'), JESTCONFIG(context), {
encoding: 'utf-8',
});
await fsp.writeFile(resolve(path, 'postcss.config.js'), POSTCSS(context), {
encoding: 'utf-8',
});
await fsp.writeFile(resolve(path, 'tsconfig.json'), TSCONFIG(context), {
encoding: 'utf-8',
});
await fsp.writeFile(
resolve(path, 'tailwind.config.js'),
TAILWINDCSS(context),
{
encoding: 'utf-8',
},
);
await fsp.writeFile(resolve(path, 'vite.config.ts'), VITE(context), {
encoding: 'utf-8',
});
}
async function appendStyle(context) {
const { path } = context;
await fsp.mkdir(resolve(path, 'style'));
await fsp.writeFile(resolve(path, 'style', 'index.css'), '', {
encoding: 'utf-8',
});
await fsp.writeFile(resolve(path, 'notailwind.css'), CSS(), {
encoding: 'utf-8',
});
}
async function adjustPlayground({ fullName, pwd, packageName }) {
await promisify(child_process.exec)(
`yarn lerna add ${fullName} --scope playground`,
);
const tmp = await fsp.readFile(resolve(pwd, 'playground', 'tsconfig.json'), {
encoding: 'utf-8',
});
const tsconfig = JSON.parse(tmp);
tsconfig.references.push({ path: `../packages/${packageName}` });
await fsp.writeFile(
resolve(pwd, 'playground', 'tsconfig.json'),
prettier.format(JSON.stringify(tsconfig), { parser: 'json' }),
{
encoding: 'utf-8',
},
);
await fsp.writeFile(
resolve(pwd, 'playground/src/packages.css'),
`\n@import "${fullName}/style";`,
{
encoding: 'utf-8',
flag: 'a+',
},
);
}
const TSCONFIG = () => `{
"extends":"../../config/tsconfig.base.json",
"include": ["src/**/*"],
"exclude": ["dist"],
"compilerOptions": {
"outDir": "dist",
},
}
`;
const JESTCONFIG = ({
packageName,
}) => `const base = require('../../config/jest.config.base');
module.exports = {
...base,
name: '${packageName}',
displayName: '${packageName}',
};
`;
const POSTCSS =
() => `const baseConfig = require('../../config/postcss.config.base');
module.exports = {
...baseConfig,
};
`;
const TAILWINDCSS =
() => `const baseConfig = require('../../config/tailwind.config.base');
module.exports = {
...baseConfig,
corePlugins: {
preflight: false,
},
};
`;
const VITE = ({
meta,
}) => `// import reactRefresh from '@vitejs/plugin-react-refresh';
// import typescript from 'rollup-plugin-typescript2';
// eslint-disable-next-line import/no-extraneous-dependencies
import { defineConfig } from 'vite';
import pkg from './package.json';
const deps = ([] as string[])
.concat(
(pkg as any).dependencies ? Object.keys((pkg as any).dependencies) : [],
)
.concat(pkg.peerDependencies ? Object.keys(pkg.peerDependencies) : []);
const name = ['${meta.name.toUpperCase()}',pkg.name.replace('@${
meta.name
}/', '').replace(/^(\s)/, (char) => char.toUpperCase())].join('');
export default defineConfig({
build: {
sourcemap: true,
lib: {
entry: pkg.source,
name,
fileName: 'index',
// formats: ['es'],
},
rollupOptions: {
external: deps,
},
},
// plugins: [reactRefresh()],
});
`;
const CSS = () => `@import "tailwindcss/base";
@import "tailwindcss/components";
@import "tailwindcss/utilities";
@import "style/index.css"
`;
main(process.argv[2]);