-
Notifications
You must be signed in to change notification settings - Fork 82
/
bootstrap.ts
144 lines (134 loc) · 3.99 KB
/
bootstrap.ts
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
import 'zx/globals';
import { PATHS, SCRIPTS } from './.internal/constants';
import { setExcludeFolder } from './.internal/utils';
(async () => {
const root = PATHS.ROOT;
const pkgDir = path.join(root, 'packages');
const pkgs = await fs.readdir(pkgDir);
for (const pkg of pkgs) {
if (pkg.charAt(0) === '.') continue;
if (!(await fs.stat(path.join(pkgDir, pkg))).isDirectory()) continue;
await bootstrapPkg({
pkgDir,
pkg,
force: argv.force,
});
}
function getName(pkgName: string) {
if (['alita'].includes(pkgName)) {
return pkgName;
} else {
return `@alita/${pkgName}`;
}
}
function getVersion() {
return '3.0.0';
}
async function bootstrapPkg(opts: any) {
const pkgDir = path.join(opts.pkgDir, opts.pkg);
if (!opts.force && fs.existsSync(path.join(pkgDir, 'package.json'))) {
console.log(`${opts.pkg} exists`);
} else {
const name = getName(opts.pkg);
// package.json
const pkgPkgJSONPath = path.join(pkgDir, 'package.json');
const hasPkgJSON = fs.existsSync(pkgPkgJSONPath);
const pkgPkgJSON = hasPkgJSON ? require(pkgPkgJSONPath) : {};
fs.writeJSONSync(
pkgPkgJSONPath,
Object.assign(
{
name,
version: getVersion(),
description: name,
main: 'dist/index.js',
types: 'dist/index.d.ts',
files: ['dist'],
scripts: {
build: SCRIPTS.BUILD,
'build:deps': SCRIPTS.BUNDLE_DEPS,
dev: SCRIPTS.DEV,
},
repository: {
type: 'git',
url: 'https://github.com/alitajs/alita',
},
authors: [
'xiaohuoni <xiaohuoni@gmail.com> (https://github.com/xiaohuoni)',
],
license: 'MIT',
bugs: 'https://github.com/alitajs/alita/issues',
homepage: `https://github.com/alitajs/alita/tree/master/packages/${opts.pkg}#readme`,
publishConfig: {
access: 'public',
},
},
{
...(hasPkgJSON
? {
authors: pkgPkgJSON.authors,
bin: pkgPkgJSON.bin,
files: pkgPkgJSON.files,
scripts: pkgPkgJSON.scripts,
description: pkgPkgJSON.description,
dependencies: pkgPkgJSON.dependencies,
devDependencies: pkgPkgJSON.devDependencies,
compiledConfig: pkgPkgJSON.compiledConfig,
}
: {}),
},
),
{ spaces: ' ' },
);
// readme 存在就不覆盖
if (!fs.existsSync(path.join(pkgDir, 'README.md'))) {
// README.md
await fs.writeFile(
path.join(pkgDir, 'README.md'),
`# ${name}\n\nSee our website [alitajs](https://alitajs.com) for more information.`,
'utf-8',
);
}
// tsconfig.json
await fs.writeFile(
path.join(pkgDir, 'tsconfig.json'),
`{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src"]
}\n`,
'utf-8',
);
// src/index.ts
const srcDir = path.join(pkgDir, 'src');
if (!fs.existsSync(srcDir)) {
await $`mkdir ${srcDir}`;
}
if (!fs.existsSync(path.join(pkgDir, 'src', 'index.ts'))) {
await fs.writeFile(
path.join(pkgDir, 'src', 'index.ts'),
`
export default () => {
return '${name}';
};\n`.trimLeft(),
'utf-8',
);
await fs.writeFile(
path.join(pkgDir, 'src', 'index.test.ts'),
`
import index from './index';
test('normal', () => {
expect(index()).toEqual('${name}');
});\n`.trimLeft(),
'utf-8',
);
}
// set excludeFolder for webstorm
setExcludeFolder({ pkg: opts.pkg, cwd: root });
console.log(chalk.green(`${opts.pkg} bootstrapped`));
}
}
})();