UNPKG

9.01 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 (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};
25Object.defineProperty(exports, "__esModule", { value: true });
26const yargs_1 = __importDefault(require("yargs"));
27const cross_spawn_extra_1 = __importDefault(require("cross-spawn-extra"));
28const fs_extra_1 = require("fs-extra");
29const path_1 = require("path");
30const workspaces_config_1 = __importStar(require("workspaces-config"));
31const npm_package_json_loader_1 = __importDefault(require("npm-package-json-loader"));
32const lib_1 = require("./lib");
33const yargs_setting_1 = __importDefault(require("./lib/yargs-setting"));
34const find_root_1 = require("@yarn-tool/find-root");
35const pkg_git_info_1 = require("@yarn-tool/pkg-git-info");
36const fs_1 = require("fs");
37const writeReadme_1 = require("./lib/writeReadme");
38//updateNotifier(__dirname);
39let cli = yargs_setting_1.default(yargs_1.default);
40let argv = cli.argv._;
41//console.dir(cli.argv);
42let cwd = path_1.resolve(cli.argv.cwd || process.cwd());
43let rootData = find_root_1.findRoot({
44 cwd,
45 skipCheckWorkspace: cli.argv.skipCheckWorkspace,
46});
47let hasWorkspace = rootData.ws;
48let workspacePrefix;
49let workspacesConfig;
50if (hasWorkspace) {
51 workspacesConfig = workspaces_config_1.parseStaticPackagesPaths(workspaces_config_1.default(hasWorkspace));
52 if (workspacesConfig.prefix.length) {
53 workspacePrefix = workspacesConfig.prefix[0];
54 }
55}
56let { targetDir, targetName } = lib_1.getTargetDir({
57 inputName: argv.length && argv[0],
58 cwd,
59 targetName: cli.argv.name || null,
60 hasWorkspace,
61 workspacePrefix,
62 workspacesConfig,
63});
64fs_extra_1.ensureDirSync(targetDir);
65let flags = Object.keys(cli.argv)
66 .reduce(function (a, f) {
67 if (f === 'silent' || f === 'y' || f === 'yes') {
68 }
69 else if (/^[a-z]$/.test(f) && cli.argv[f]) {
70 a.push(f);
71 }
72 return a;
73}, [])
74 .join('');
75let args = [
76 'init',
77 (flags && '-' + flags),
78 cli.argv.createModule,
79 cli.argv.yes && '-y',
80].filter(v => v);
81//console.log(args);
82let old_pkg_name;
83let oldExists = fs_1.existsSync(path_1.join(targetDir, 'package.json'));
84if (!targetName) {
85 try {
86 let pkg = new npm_package_json_loader_1.default(path_1.join(targetDir, 'package.json'));
87 old_pkg_name = pkg.data.name;
88 }
89 catch (e) {
90 }
91}
92let cp = cross_spawn_extra_1.default.sync(cli.argv.npmClient, args, {
93 stdio: 'inherit',
94 cwd: targetDir,
95});
96if (!cp.error) {
97 let pkg = new npm_package_json_loader_1.default(path_1.join(targetDir, 'package.json'));
98 if (pkg.exists()) {
99 if (cli.argv.p && cli.argv.npmClient != 'yarn') {
100 pkg.data.private = true;
101 }
102 // 防止 node- 被 npm 移除
103 if (!cli.argv.yes && old_pkg_name && /^node-/.test(old_pkg_name) && ('node-' + pkg.data.name) === old_pkg_name) {
104 pkg.data.name = old_pkg_name;
105 }
106 else if (cli.argv.yes && old_pkg_name && pkg.data.name != old_pkg_name) {
107 pkg.data.name = old_pkg_name;
108 }
109 else if (targetName && pkg.data.name != targetName) {
110 pkg.data.name = targetName;
111 }
112 if (pkg.data.name && /^@/.test(pkg.data.name) && !pkg.data.publishConfig) {
113 //pkg.data.publishConfig = {};
114 }
115 if (!pkg.data.scripts) {
116 pkg.data.scripts = {};
117 }
118 if (!pkg.data.homepage || !pkg.data.bugs || !pkg.data.repository) {
119 try {
120 let info = pkg_git_info_1.npmHostedGitInfo(targetDir);
121 // @ts-ignore
122 pkg.data.homepage = pkg.data.homepage || info.homepage;
123 if (hasWorkspace) {
124 let u = new URL(pkg.data.homepage);
125 u.pathname += '/tree/master/' + path_1.relative(hasWorkspace, targetDir);
126 // @ts-ignore
127 pkg.data.homepage = u.toString();
128 }
129 pkg.data.bugs = pkg.data.bugs || {
130 url: info.bugs,
131 };
132 pkg.data.repository = pkg.data.repository || {
133 "type": "git",
134 url: info.repository,
135 };
136 }
137 catch (e) {
138 }
139 }
140 if (!oldExists) {
141 Object
142 .entries({
143 "test:mocha": "npx mocha --require ts-node/register \"!(node_modules)/**/*.{test,spec}.{ts,tsx}\"",
144 "test:jest": "jest --coverage",
145 "prepublish:lockfile": "npx sync-lockfile .",
146 "lint": "npx eslint **/*.ts",
147 "ncu": "npx yarn-tool ncu -u",
148 "npm:publish": "npm publish",
149 "npm:publish:lerna": "lerna publish --yes",
150 "tsc:default": "tsc -p tsconfig.json",
151 "tsc:esm": "tsc -p tsconfig.esm.json",
152 "sort-package-json": "npx yarn-tool sort",
153 "prepublishOnly_": "yarn run ncu && yarn run sort-package-json && yarn run test",
154 "postpublish_": `git commit -m "chore(release): publish" .`,
155 "coverage": "npx nyc yarn run test",
156 "test": `echo "Error: no test specified" && exit 1`,
157 })
158 .forEach(([k, v]) => {
159 if (pkg.data.scripts[k] == null) {
160 pkg.data.scripts[k] = v;
161 }
162 });
163 }
164 else {
165 Object
166 .entries({
167 "prepublish:lockfile": "npx sync-lockfile .",
168 "ncu": "npx yarn-tool ncu -u",
169 "npm:publish": "npm publish",
170 "sort-package-json": "npx yarn-tool sort",
171 "prepublishOnly_": "yarn run ncu && yarn run sort-package-json && yarn run test",
172 "postpublish_": `git commit -m "chore(release): publish" .`,
173 })
174 .forEach(([k, v]) => {
175 if (k.endsWith('_') && pkg.data.scripts[k.replace(/_+$/, '')] === v) {
176 return;
177 }
178 if (pkg.data.scripts[k] == null) {
179 pkg.data.scripts[k] = v;
180 }
181 });
182 }
183 if (!oldExists) {
184 const cpkg = require('./package.json');
185 const findVersion = (name) => {
186 var _a, _b, _c;
187 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]) || "*";
188 };
189 pkg.data.devDependencies = pkg.data.devDependencies || {};
190 pkg.data.devDependencies['@bluelovers/tsconfig'] = findVersion('@bluelovers/tsconfig');
191 pkg.data.devDependencies['@types/node'] = findVersion('@types/node');
192 }
193 pkg.autofix();
194 if (cli.argv.sort) {
195 pkg.sort();
196 }
197 pkg.writeOnlyWhenLoaded();
198 try {
199 let copyOptions = {
200 overwrite: false,
201 preserveTimestamps: true,
202 errorOnExist: false,
203 };
204 fs_extra_1.copySync(path_1.join(__dirname, 'lib/static'), targetDir, copyOptions);
205 }
206 catch (e) {
207 }
208 lib_1.copyStaticFiles(lib_1.defaultCopyStaticFiles, {
209 cwd: targetDir,
210 });
211 (!oldExists) && writeReadme_1.writeReadme({
212 file: path_1.join(targetDir, 'README.md'),
213 variable: pkg.data,
214 });
215 /*
216 fs.copySync(path.join(__dirname, 'lib/file/npmignore'), path.join(targetDir, '.npmignore'), copyOptions);
217
218 fs.copySync(path.join(__dirname, 'lib/file/gitignore'), path.join(targetDir, '.gitignore'), copyOptions);
219
220 if (!fs.pathExistsSync(path.join(targetDir, 'tsconfig.json')))
221 {
222 fs.copySync(path.join(__dirname, 'lib/file/tsconfig.json.tpl'), path.join(targetDir, 'tsconfig.json.tpl'), copyOptions);
223 }
224 */
225 }
226}
227else {
228 process.exitCode = 1;
229}
230//# sourceMappingURL=index.js.map
\No newline at end of file