UNPKG

10.6 kBJavaScriptView Raw
1#!/usr/bin/env node
2"use strict";
3var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4 if (k2 === undefined) k2 = k;
5 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
6}) : (function(o, m, k, k2) {
7 if (k2 === undefined) k2 = k;
8 o[k2] = m[k];
9}));
10var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
11 Object.defineProperty(o, "default", { enumerable: true, value: v });
12}) : function(o, v) {
13 o["default"] = v;
14});
15var __importStar = (this && this.__importStar) || function (mod) {
16 if (mod && mod.__esModule) return mod;
17 var result = {};
18 if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
19 __setModuleDefault(result, mod);
20 return result;
21};
22var __importDefault = (this && this.__importDefault) || function (mod) {
23 return (mod && mod.__esModule) ? mod : { "default": mod };
24};
25var _a, _b, _c;
26Object.defineProperty(exports, "__esModule", { value: true });
27const yargs_1 = __importDefault(require("yargs"));
28const cross_spawn_extra_1 = __importDefault(require("cross-spawn-extra"));
29const fs_extra_1 = require("fs-extra");
30const path_1 = require("path");
31const workspaces_config_1 = __importStar(require("workspaces-config"));
32const npm_package_json_loader_1 = __importDefault(require("npm-package-json-loader"));
33const lib_1 = require("./lib");
34const yargs_setting_1 = __importDefault(require("./lib/yargs-setting"));
35const find_root_1 = require("@yarn-tool/find-root");
36const pkg_git_info_1 = require("@yarn-tool/pkg-git-info");
37const fs_1 = require("fs");
38const writeReadme_1 = require("./lib/writeReadme");
39//updateNotifier(__dirname);
40let cli = yargs_setting_1.default(yargs_1.default);
41let argv = cli.argv._;
42//console.dir(cli.argv);
43let cwd = path_1.resolve(cli.argv.cwd || process.cwd());
44let rootData = find_root_1.findRoot({
45 cwd,
46 skipCheckWorkspace: cli.argv.skipCheckWorkspace,
47});
48let hasWorkspace = rootData.ws;
49let workspacePrefix;
50let workspacesConfig;
51if (hasWorkspace) {
52 workspacesConfig = workspaces_config_1.parseStaticPackagesPaths(workspaces_config_1.default(hasWorkspace));
53 if (workspacesConfig.prefix.length) {
54 workspacePrefix = workspacesConfig.prefix[0];
55 }
56}
57let { targetDir, targetName } = lib_1.getTargetDir({
58 inputName: argv.length && argv[0],
59 cwd,
60 targetName: cli.argv.name || null,
61 hasWorkspace,
62 workspacePrefix,
63 workspacesConfig,
64});
65fs_extra_1.ensureDirSync(targetDir);
66let flags = Object.keys(cli.argv)
67 .reduce(function (a, f) {
68 if (f === 'silent' || f === 'y' || f === 'yes') {
69 }
70 else if (/^[a-z]$/.test(f) && cli.argv[f]) {
71 a.push(f);
72 }
73 return a;
74}, [])
75 .join('');
76let args = [
77 'init',
78 (flags && '-' + flags),
79 cli.argv.createModule,
80 cli.argv.yes && '-y',
81].filter(v => v);
82//console.log(args);
83let old_pkg_name;
84let oldExists = fs_1.existsSync(path_1.join(targetDir, 'package.json'));
85let old_pkg;
86if (!targetName) {
87 try {
88 old_pkg = (_a = new npm_package_json_loader_1.default(path_1.join(targetDir, 'package.json'))) === null || _a === void 0 ? void 0 : _a.data;
89 old_pkg_name = old_pkg.name;
90 }
91 catch (e) {
92 }
93}
94let cp = cross_spawn_extra_1.default.sync(cli.argv.npmClient, args, {
95 stdio: 'inherit',
96 cwd: targetDir,
97});
98if (!cp.error) {
99 let pkg = new npm_package_json_loader_1.default(path_1.join(targetDir, 'package.json'));
100 if (pkg.exists()) {
101 if (cli.argv.p && cli.argv.npmClient != 'yarn') {
102 pkg.data.private = true;
103 }
104 // 防止 node- 被 npm 移除
105 if (!cli.argv.yes && old_pkg_name && /^node-/.test(old_pkg_name) && ('node-' + pkg.data.name) === old_pkg_name) {
106 pkg.data.name = old_pkg_name;
107 }
108 else if (cli.argv.yes && old_pkg_name && pkg.data.name != old_pkg_name) {
109 pkg.data.name = old_pkg_name;
110 }
111 else if (targetName && pkg.data.name != targetName) {
112 pkg.data.name = targetName;
113 }
114 if (pkg.data.name && /^@/.test(pkg.data.name) && !pkg.data.publishConfig) {
115 //pkg.data.publishConfig = {};
116 }
117 if (!pkg.data.scripts) {
118 pkg.data.scripts = {};
119 }
120 if (!pkg.data.homepage || !pkg.data.bugs || !pkg.data.repository) {
121 try {
122 let info = pkg_git_info_1.npmHostedGitInfo(targetDir);
123 // @ts-ignore
124 pkg.data.homepage = pkg.data.homepage || info.homepage;
125 if (hasWorkspace) {
126 let u = new URL(pkg.data.homepage);
127 u.pathname += '/tree/master/' + path_1.relative(hasWorkspace, targetDir);
128 // @ts-ignore
129 pkg.data.homepage = u.toString();
130 }
131 pkg.data.bugs = pkg.data.bugs || {
132 url: info.bugs,
133 };
134 pkg.data.repository = pkg.data.repository || {
135 "type": "git",
136 url: info.repository,
137 };
138 }
139 catch (e) {
140 }
141 }
142 let sharedScript = {
143 "prepublishOnly:check-bin": "ynpx --quiet @yarn-tool/check-pkg-bin",
144 "prepublishOnly:update": "yarn run ncu && yarn run sort-package-json",
145 "ncu": "ynpx --quiet yarn-tool -- ncu -u",
146 "sort-package-json": "ynpx --quiet yarn-tool -- sort",
147 "test": `echo "Error: no test specified"`,
148 };
149 let prepublishOnly = "yarn run prepublishOnly:check-bin && yarn run prepublishOnly:update && yarn run test";
150 if (hasWorkspace) {
151 prepublishOnly = "yarn run prepublishOnly:check-bin && yarn run test";
152 sharedScript = {
153 ...sharedScript,
154 };
155 }
156 else {
157 sharedScript = {
158 ...sharedScript,
159 "npm:publish": "npm publish",
160 "npm:publish:lerna": "ynpx --quiet lerna -- publish --yes --bump patch",
161 "postpublish:git:commit": `git commit -m "chore(release): publish" . & echo postpublish:git:commit`,
162 "postpublish:git:tag": `ynpx --quiet @yarn-tool/tag`,
163 "postpublish:changelog": `ynpx --quiet @yarn-tool/changelog && git add ./CHANGELOG.md`,
164 "postpublish:git:push": `git push --follow-tags`,
165 "postpublish_": `yarn run postpublish:changelog && yarn run postpublish:git:commit && yarn run postpublish:git:tag && yarn run postpublish:git:push`,
166 };
167 if (!oldExists) {
168 sharedScript = {
169 ...sharedScript,
170 "coverage": "ynpx --quiet nyc -- npm run test",
171 "tsc:default": "tsc -p tsconfig.json",
172 "tsc:esm": "tsc -p tsconfig.esm.json",
173 };
174 }
175 }
176 if (oldExists) {
177 sharedScript.prepublishOnly_ = prepublishOnly;
178 }
179 else {
180 sharedScript.prepublishOnly = prepublishOnly;
181 }
182 if (!oldExists) {
183 if (((_b = pkg.data.scripts) === null || _b === void 0 ? void 0 : _b.test) === "echo \"Error: no test specified\" && exit 1" && ((_c = sharedScript.test) === null || _c === void 0 ? void 0 : _c.length) > 0) {
184 delete pkg.data.scripts.test;
185 }
186 Object
187 .entries({
188 "test:mocha": "ynpx --quiet -p ts-node -p mocha mocha -- --require ts-node/register \"!(node_modules)/**/*.{test,spec}.{ts,tsx}\"",
189 "test:jest": "ynpx --quiet jest -- --coverage",
190 "lint": "ynpx --quiet eslint -- **/*.ts",
191 ...sharedScript,
192 })
193 .forEach(([k, v]) => {
194 if (pkg.data.scripts[k] == null) {
195 pkg.data.scripts[k] = v;
196 }
197 });
198 }
199 else {
200 Object
201 .entries(sharedScript)
202 .forEach(([k, v]) => {
203 if (k.endsWith('_') && pkg.data.scripts[k.replace(/_+$/, '')] === v) {
204 return;
205 }
206 if (pkg.data.scripts[k] == null) {
207 pkg.data.scripts[k] = v;
208 }
209 });
210 if (old_pkg) {
211 Object.keys(old_pkg)
212 .forEach(key => {
213 if (!(key in pkg.data)) {
214 pkg.data[key] = old_pkg[key];
215 }
216 });
217 }
218 }
219 if (!oldExists) {
220 const cpkg = require('./package.json');
221 const findVersion = (name) => {
222 var _a, _b, _c;
223 return ((_a = cpkg.dependencies) === null || _a === void 0 ? void 0 : _a[name]) || ((_b = cpkg.devDependencies) === null || _b === void 0 ? void 0 : _b[name]) || ((_c = cpkg.peerDependencies) === null || _c === void 0 ? void 0 : _c[name]) || "*";
224 };
225 pkg.data.devDependencies = pkg.data.devDependencies || {};
226 pkg.data.devDependencies['@bluelovers/tsconfig'] = findVersion('@bluelovers/tsconfig');
227 pkg.data.devDependencies['@types/node'] = findVersion('@types/node');
228 }
229 pkg.autofix();
230 if (cli.argv.sort) {
231 pkg.sort();
232 }
233 pkg.writeOnlyWhenLoaded();
234 try {
235 let copyOptions = {
236 overwrite: false,
237 preserveTimestamps: true,
238 errorOnExist: false,
239 };
240 fs_extra_1.copySync(path_1.join(__dirname, 'lib/static'), targetDir, copyOptions);
241 }
242 catch (e) {
243 }
244 lib_1.copyStaticFiles(lib_1.defaultCopyStaticFiles, {
245 cwd: targetDir,
246 });
247 (!oldExists) && writeReadme_1.writeReadme({
248 file: path_1.join(targetDir, 'README.md'),
249 variable: pkg.data,
250 });
251 /*
252 fs.copySync(path.join(__dirname, 'lib/file/npmignore'), path.join(targetDir, '.npmignore'), copyOptions);
253
254 fs.copySync(path.join(__dirname, 'lib/file/gitignore'), path.join(targetDir, '.gitignore'), copyOptions);
255
256 if (!fs.pathExistsSync(path.join(targetDir, 'tsconfig.json')))
257 {
258 fs.copySync(path.join(__dirname, 'lib/file/tsconfig.json.tpl'), path.join(targetDir, 'tsconfig.json.tpl'), copyOptions);
259 }
260 */
261 }
262}
263else {
264 process.exitCode = 1;
265}
266//# sourceMappingURL=index.js.map
\No newline at end of file