UNPKG

4.93 kBJavaScriptView Raw
1"use strict";
2/**
3 * Created by user on 2018/11/28/028.
4 */
5var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
8}) : (function(o, m, k, k2) {
9 if (k2 === undefined) k2 = k;
10 o[k2] = m[k];
11}));
12var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
13 Object.defineProperty(o, "default", { enumerable: true, value: v });
14}) : function(o, v) {
15 o["default"] = v;
16});
17var __importStar = (this && this.__importStar) || function (mod) {
18 if (mod && mod.__esModule) return mod;
19 var result = {};
20 if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
21 __setModuleDefault(result, mod);
22 return result;
23};
24var __importDefault = (this && this.__importDefault) || function (mod) {
25 return (mod && mod.__esModule) ? mod : { "default": mod };
26};
27Object.defineProperty(exports, "__esModule", { value: true });
28exports.copyStaticFiles = exports.defaultCopyStaticFiles = exports.validateNpmPackageName = exports.getTargetDir = exports.npmVersion = void 0;
29const cross_spawn_extra_1 = __importDefault(require("cross-spawn-extra"));
30const json5_1 = __importDefault(require("json5"));
31const validate_npm_package_name_1 = __importDefault(require("validate-npm-package-name"));
32const fs_extra_1 = require("fs-extra");
33const path_1 = require("path");
34const static_file_1 = __importStar(require("@yarn-tool/static-file"));
35Object.defineProperty(exports, "defaultCopyStaticFiles", { enumerable: true, get: function () { return static_file_1.defaultCopyStaticFiles; } });
36const searchWorkspacePrefixByName_1 = __importDefault(require("./searchWorkspacePrefixByName"));
37function npmVersion(npmClient, cwd) {
38 let args = [
39 'version',
40 ];
41 npmClient = npmClient || 'npm';
42 if (npmClient === 'yarn') {
43 args = [
44 'versions',
45 ];
46 }
47 let cp = cross_spawn_extra_1.default.sync(npmClient, args, {
48 cwd,
49 stripAnsi: true,
50 });
51 if (cp.error) {
52 throw cp.error;
53 }
54 let output = cp.stdout.toString()
55 .replace(/^yarn versions [^\n]+$/gm, '')
56 .replace(/^Done in [^\n]+$/gm, '')
57 .replace(/^\s+|\s+$/g, '');
58 let json = json5_1.default.parse(output);
59 return json;
60}
61exports.npmVersion = npmVersion;
62function getTargetDir(options) {
63 var _a;
64 let targetDir;
65 let targetName = options.targetName || null;
66 let { inputName, cwd, hasWorkspace, workspacePrefix, workspacesConfig } = options;
67 if (hasWorkspace && !((_a = workspacesConfig === null || workspacesConfig === void 0 ? void 0 : workspacesConfig.prefix) === null || _a === void 0 ? void 0 : _a.length)) {
68 throw new RangeError(`can't found workspace prefix`);
69 }
70 if (targetName) {
71 validateNpmPackageName(targetName, true);
72 }
73 if (inputName) {
74 targetName = targetName || inputName;
75 let ret = validateNpmPackageName(inputName, true);
76 let name = inputName;
77 let basePath;
78 if (hasWorkspace) {
79 const workspacePrefix = searchWorkspacePrefixByName_1.default({
80 inputName,
81 workspacesConfig,
82 });
83 basePath = path_1.join(hasWorkspace, workspacePrefix);
84 }
85 else {
86 basePath = cwd;
87 }
88 if (ret.scopedPackagePattern) {
89 name = name
90 .replace(/[\/\\]+/g, '_')
91 .replace(/^@/g, '');
92 if (!fs_extra_1.pathExistsSync(path_1.join(basePath, ret.subname))) {
93 name = ret.subname;
94 }
95 }
96 targetDir = path_1.resolve(basePath, name);
97 }
98 else {
99 targetDir = cwd;
100 }
101 return {
102 targetDir,
103 targetName,
104 cwd,
105 };
106}
107exports.getTargetDir = getTargetDir;
108const scopedPackagePattern = new RegExp('^(?:@([^/]+?)[/])?([^/]+?)$');
109function validateNpmPackageName(name, throwErr) {
110 let ret = validate_npm_package_name_1.default(name);
111 ret.name = name;
112 if (!ret.errors || !ret.errors.length) {
113 const nameMatch = name.match(scopedPackagePattern);
114 if (nameMatch) {
115 ret.scopedPackagePattern = true;
116 ret.user = nameMatch[1];
117 ret.subname = nameMatch[2];
118 }
119 else {
120 ret.scopedPackagePattern = false;
121 }
122 }
123 else if (throwErr) {
124 throw new RangeError(ret.errors.concat(ret.warnings || []).join(' ; '));
125 }
126 return ret;
127}
128exports.validateNpmPackageName = validateNpmPackageName;
129function copyStaticFiles(file_map, options) {
130 return static_file_1.default({
131 ...options,
132 file_map,
133 });
134}
135exports.copyStaticFiles = copyStaticFiles;
136//# sourceMappingURL=index.js.map
\No newline at end of file