UNPKG

10.1 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21var __importDefault = (this && this.__importDefault) || function (mod) {
22 return (mod && mod.__esModule) ? mod : { "default": mod };
23};
24Object.defineProperty(exports, "__esModule", { value: true });
25exports.getPublishTag = exports.getRemotePackageVersion = exports.fixDepPkgName = exports.getDocPath = exports.updatePkgJSON = exports.updatePkgInfo = exports.getPkgNames = exports.getPkgInfo = exports.findPackageByName = exports.readPackageInfo = exports.addPackageConfig = exports.isMainPackage = exports.getMainPackageInfo = exports.getJestAliases = exports.getWorkspaceNames = exports.listPackages = exports.getE2EDir = void 0;
26const fs_1 = __importDefault(require("fs"));
27const path_1 = __importDefault(require("path"));
28const fs_extra_1 = __importDefault(require("fs-extra"));
29const semver_1 = __importDefault(require("semver"));
30const globby_1 = __importDefault(require("globby"));
31const utils_1 = require("@terascope/utils");
32// @ts-expect-error (Missing types)
33const query_graph_1 = __importDefault(require("@lerna/query-graph"));
34const package_json_1 = __importDefault(require("package-json"));
35const sort_package_json_1 = __importDefault(require("sort-package-json"));
36const misc = __importStar(require("./misc"));
37const i = __importStar(require("./interfaces"));
38let _packages = [];
39let _e2eDir;
40function getE2EDir() {
41 if (_e2eDir)
42 return _e2eDir;
43 if (fs_1.default.existsSync(path_1.default.join(misc.getRootDir(), 'e2e'))) {
44 _e2eDir = path_1.default.join(misc.getRootDir(), 'e2e');
45 return _e2eDir;
46 }
47 return undefined;
48}
49exports.getE2EDir = getE2EDir;
50function _loadPackage(packagePath) {
51 const pkgJsonPath = path_1.default.join(packagePath, 'package.json');
52 if (fs_1.default.existsSync(pkgJsonPath)) {
53 return readPackageInfo(packagePath);
54 }
55 return undefined;
56}
57function _resolveWorkspaces(workspaces, rootDir) {
58 return workspaces
59 .reduce((pkgDirs, pkgGlob) => [
60 ...pkgDirs,
61 ...(globby_1.default.hasMagic(pkgGlob)
62 ? globby_1.default.sync(path_1.default.join(rootDir, pkgGlob), {
63 onlyDirectories: true,
64 })
65 : [path_1.default.join(rootDir, pkgGlob)]),
66 ], []);
67}
68function listPackages() {
69 if (_packages && _packages.length)
70 return _packages.slice();
71 const rootPkg = misc.getRootInfo();
72 if (!rootPkg.workspaces)
73 return [];
74 const workspaces = (Array.isArray(rootPkg.workspaces)
75 ? rootPkg.workspaces
76 : rootPkg.workspaces.packages).slice();
77 if (!workspaces)
78 return [];
79 const hasE2E = workspaces.find((workspacePath) => workspacePath.includes('e2e'));
80 if (!hasE2E) {
81 workspaces.push('e2e');
82 }
83 const workspacePaths = _resolveWorkspaces(workspaces, misc.getRootDir());
84 const packages = workspacePaths
85 .map(_loadPackage)
86 .filter((pkg) => pkg === null || pkg === void 0 ? void 0 : pkg.name);
87 _packages = query_graph_1.default.toposort(packages);
88 return _packages;
89}
90exports.listPackages = listPackages;
91function getWorkspaceNames() {
92 return utils_1.uniq(listPackages()
93 .filter((pkg) => !('workspaces' in pkg))
94 .map((pkg) => path_1.default.basename(path_1.default.dirname(pkg.dir)))
95 .filter((name) => name && name !== '.'));
96}
97exports.getWorkspaceNames = getWorkspaceNames;
98function getJestAliases() {
99 const aliases = {};
100 listPackages().forEach((pkg) => {
101 const key = `^${pkg.name}$`;
102 const mainFile = pkg.srcMain || pkg.main;
103 if (!mainFile)
104 return;
105 aliases[key] = path_1.default.join(pkg.dir, mainFile);
106 });
107 return aliases;
108}
109exports.getJestAliases = getJestAliases;
110function getMainPackageInfo() {
111 return listPackages().find(isMainPackage);
112}
113exports.getMainPackageInfo = getMainPackageInfo;
114function isMainPackage(pkgInfo) {
115 return utils_1.get(pkgInfo, 'terascope.main', false);
116}
117exports.isMainPackage = isMainPackage;
118function addPackageConfig(pkgInfo) {
119 for (const _key of Object.keys(pkgInfo.terascope)) {
120 const key = _key;
121 if (!i.AvailablePackageConfigKeys.includes(key)) {
122 throw new Error(`Unknown terascope config "${key}" found in "${pkgInfo.name}" package`);
123 }
124 }
125}
126exports.addPackageConfig = addPackageConfig;
127function readPackageInfo(folderPath) {
128 const dir = path_1.default.isAbsolute(folderPath)
129 ? path_1.default.join(folderPath)
130 : path_1.default.join(misc.getRootDir(), folderPath);
131 const pkgJSONPath = path_1.default.join(dir, 'package.json');
132 const pkgJSON = getSortedPkgJSON(fs_extra_1.default.readJSONSync(pkgJSONPath));
133 pkgJSON.dir = dir;
134 updatePkgInfo(pkgJSON);
135 return pkgJSON;
136}
137exports.readPackageInfo = readPackageInfo;
138/** A stricter version of `getPkgInfo` */
139function findPackageByName(packages, name) {
140 const found = packages.find((info) => info.name === name);
141 if (!found) {
142 throw new Error(`Unable to find package ${name}`);
143 }
144 return found;
145}
146exports.findPackageByName = findPackageByName;
147function getPkgInfo(name) {
148 const found = listPackages().find((info) => [info.name, info.folderName].includes(name));
149 if (!found) {
150 throw new Error(`Unable to find package ${name}`);
151 }
152 return found;
153}
154exports.getPkgInfo = getPkgInfo;
155function getPkgNames(packages) {
156 return utils_1.uniq(packages.map((pkgInfo) => pkgInfo.folderName)).sort();
157}
158exports.getPkgNames = getPkgNames;
159function updatePkgInfo(pkgInfo) {
160 if (!pkgInfo.dir) {
161 throw new Error('Missing dir on package.json reference');
162 }
163 if (!pkgInfo.terascope)
164 pkgInfo.terascope = {};
165 if (pkgInfo.terascope.enableTypedoc && !fs_1.default.existsSync(path_1.default.join(pkgInfo.dir, 'tsconfig.json'))) {
166 pkgInfo.terascope.enableTypedoc = false;
167 }
168 const rootInfo = misc.getRootInfo();
169 if (!pkgInfo.private) {
170 if (!pkgInfo.publishConfig) {
171 pkgInfo.publishConfig = {
172 access: 'public',
173 registry: rootInfo.terascope.npm.registry,
174 };
175 }
176 else {
177 pkgInfo.publishConfig = Object.assign({}, {
178 access: 'public',
179 registry: rootInfo.terascope.npm.registry,
180 }, pkgInfo.publishConfig);
181 }
182 }
183 else {
184 delete pkgInfo.publishConfig;
185 }
186 pkgInfo.folderName = path_1.default.basename(pkgInfo.dir);
187 pkgInfo.relativeDir = path_1.default.relative(rootInfo.dir, pkgInfo.dir);
188 addPackageConfig(pkgInfo);
189 if (!pkgInfo.displayName) {
190 pkgInfo.displayName = misc.getName(pkgInfo.folderName);
191 }
192 if (!pkgInfo.license) {
193 pkgInfo.license = 'MIT';
194 }
195}
196exports.updatePkgInfo = updatePkgInfo;
197function updatePkgJSON(pkgInfo, log) {
198 if (!utils_1.get(pkgInfo, 'terascope.root')) {
199 updatePkgInfo(pkgInfo);
200 }
201 const pkgJSON = getSortedPkgJSON(pkgInfo);
202 delete pkgJSON.folderName;
203 delete pkgJSON.dir;
204 delete pkgJSON.relativeDir;
205 return misc.writeIfChanged(path_1.default.join(pkgInfo.dir, 'package.json'), pkgJSON, {
206 log,
207 });
208}
209exports.updatePkgJSON = updatePkgJSON;
210function getSortedPkgJSON(pkgInfo) {
211 return utils_1.fastCloneDeep(sort_package_json_1.default(pkgInfo));
212}
213function getDocPath(pkgInfo, withFileName, withExt = true) {
214 const suite = pkgInfo.terascope.testSuite;
215 if (suite === 'e2e') {
216 const e2eDevDocs = path_1.default.join('docs/development');
217 fs_extra_1.default.ensureDirSync(e2eDevDocs);
218 if (withFileName) {
219 return path_1.default.join(e2eDevDocs, withExt ? `${pkgInfo.folderName}.md` : pkgInfo.folderName);
220 }
221 return e2eDevDocs;
222 }
223 const docPath = path_1.default.join('docs', pkgInfo.relativeDir);
224 fs_extra_1.default.ensureDirSync(docPath);
225 if (withFileName) {
226 return path_1.default.join(docPath, withExt ? 'overview.md' : 'overview');
227 }
228 return docPath;
229}
230exports.getDocPath = getDocPath;
231function fixDepPkgName(name) {
232 return utils_1.trim(name).replace(/^\*\*\//, '').trim();
233}
234exports.fixDepPkgName = fixDepPkgName;
235async function getRemotePackageVersion(pkgInfo) {
236 if (pkgInfo.private)
237 return pkgInfo.version;
238 const registryUrl = utils_1.get(pkgInfo, 'publishConfig.registry');
239 const tag = getPublishTag(pkgInfo.version);
240 try {
241 const { version } = await package_json_1.default(pkgInfo.name, {
242 version: tag,
243 registryUrl
244 });
245 return version;
246 }
247 catch (err) {
248 if (err instanceof package_json_1.default.VersionNotFoundError) {
249 return pkgInfo.version;
250 }
251 if (err instanceof package_json_1.default.PackageNotFoundError) {
252 return '0.1.0';
253 }
254 throw err;
255 }
256}
257exports.getRemotePackageVersion = getRemotePackageVersion;
258function getPublishTag(version) {
259 const parsed = semver_1.default.parse(version);
260 if (!parsed) {
261 throw new Error(`Unable to publish invalid version "${version}"`);
262 }
263 if (parsed.prerelease.length)
264 return 'prerelease';
265 return 'latest';
266}
267exports.getPublishTag = getPublishTag;
268//# sourceMappingURL=packages.js.map
\No newline at end of file