UNPKG

6.85 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.writePkgHeader = exports.writeHeader = exports.formatList = exports.writeIfChanged = exports.listMdFiles = exports.getName = exports.getDevDockerImage = exports.getServicesForSuite = exports.getAvailableTestSuites = exports.getRootInfo = exports.getRootDir = void 0;
7const path_1 = __importDefault(require("path"));
8const pkg_up_1 = __importDefault(require("pkg-up"));
9const fs_extra_1 = __importDefault(require("fs-extra"));
10const lodash_defaultsdeep_1 = __importDefault(require("lodash.defaultsdeep"));
11const utils_1 = require("@terascope/utils");
12const sort_package_json_1 = __importDefault(require("sort-package-json"));
13const interfaces_1 = require("./interfaces");
14const config_1 = require("./config");
15const signale_1 = __importDefault(require("./signale"));
16let rootDir;
17function getRootDir(cwd = process.cwd()) {
18 if (rootDir)
19 return rootDir;
20 const rootPkgJSON = pkg_up_1.default.sync({ cwd });
21 if (!rootPkgJSON) {
22 throw new Error('Unable to find root directory, run in the root of the repo');
23 }
24 if (_getRootInfo(rootPkgJSON) != null) {
25 rootDir = path_1.default.dirname(rootPkgJSON);
26 return rootDir;
27 }
28 const upOne = path_1.default.join(path_1.default.dirname(rootPkgJSON), '..');
29 if (!fs_extra_1.default.existsSync(upOne) || !fs_extra_1.default.statSync(upOne).isDirectory()) {
30 throw new Error('Unable to find root directory');
31 }
32 return getRootDir(upOne);
33}
34exports.getRootDir = getRootDir;
35function _getRootInfo(pkgJSONPath) {
36 const pkg = fs_extra_1.default.readJSONSync(pkgJSONPath);
37 const isRoot = utils_1.get(pkg, 'terascope.root', false);
38 if (!isRoot)
39 return undefined;
40 const dir = path_1.default.dirname(pkgJSONPath);
41 const folderName = path_1.default.basename(dir);
42 return sort_package_json_1.default(lodash_defaultsdeep_1.default(pkg, {
43 dir,
44 relativeDir: '.',
45 folderName,
46 displayName: getName(pkg.name),
47 documentation: '',
48 homepage: '',
49 bugs: {
50 url: '',
51 },
52 terascope: {
53 root: isRoot,
54 type: 'monorepo',
55 target: 'es2018',
56 tests: {
57 suites: {}
58 },
59 docker: {
60 registries: [`terascope/${folderName}`],
61 },
62 npm: {
63 registry: config_1.NPM_DEFAULT_REGISTRY
64 }
65 },
66 }));
67}
68let _rootInfo;
69function getRootInfo() {
70 if (_rootInfo)
71 return _rootInfo;
72 _rootInfo = _getRootInfo(path_1.default.join(getRootDir(), 'package.json'));
73 return _rootInfo;
74}
75exports.getRootInfo = getRootInfo;
76function getAvailableTestSuites() {
77 return Object.keys(getRootInfo().terascope.tests.suites);
78}
79exports.getAvailableTestSuites = getAvailableTestSuites;
80function getServicesForSuite(suite) {
81 const services = getRootInfo().terascope.tests.suites[suite] || [];
82 const invalidServices = services.filter((name) => !Object.values(interfaces_1.Service).includes(name));
83 if (invalidServices.length) {
84 const actual = invalidServices.join(', ');
85 const expected = Object.values(interfaces_1.Service).join(', ');
86 throw new Error(`Unsupported service(s) ${actual}, expected ${expected}`);
87 }
88 return services;
89}
90exports.getServicesForSuite = getServicesForSuite;
91function getDevDockerImage() {
92 const rootInfo = getRootInfo();
93 const [registry] = rootInfo.terascope.docker.registries;
94 return `${registry}:dev-${config_1.DEV_TAG}`;
95}
96exports.getDevDockerImage = getDevDockerImage;
97function getName(input) {
98 return utils_1.toTitleCase(input);
99}
100exports.getName = getName;
101function listMdFiles(dir, levels = 10) {
102 if (levels < 1)
103 return [];
104 if (!dir || !fs_extra_1.default.statSync(dir).isDirectory()) {
105 throw new Error(`Invalid directory "${dir}" given to listMdFiles`);
106 }
107 const files = [];
108 for (const fileName of fs_extra_1.default.readdirSync(dir)) {
109 const filePath = path_1.default.join(dir, fileName);
110 if (fs_extra_1.default.statSync(filePath).isDirectory()) {
111 files.push(...listMdFiles(filePath, levels - 1));
112 }
113 else if (path_1.default.extname(fileName) === '.md') {
114 files.push(filePath);
115 }
116 }
117 return files;
118}
119exports.listMdFiles = listMdFiles;
120async function writeIfChanged(filePath, contents, options = {}) {
121 if (options.mkdir) {
122 await fs_extra_1.default.ensureDir(path_1.default.dirname(filePath));
123 }
124 const exists = fs_extra_1.default.existsSync(filePath);
125 if (exists && !fs_extra_1.default.statSync(filePath).isFile()) {
126 throw new Error('Unable to write to non-file type');
127 }
128 if (!contents) {
129 if (exists) {
130 await fs_extra_1.default.unlink(filePath);
131 }
132 return true;
133 }
134 if (typeof contents === 'string') {
135 const _contents = `${contents.trim()}\n`;
136 if (exists) {
137 const existing = await fs_extra_1.default.readFile(filePath, 'utf8');
138 if (existing === _contents) {
139 return false;
140 }
141 }
142 if (options.log !== false) {
143 signale_1.default.debug(`wrote ${path_1.default.relative(getRootDir(), filePath)} file`);
144 }
145 await fs_extra_1.default.writeFile(filePath, _contents, 'utf8');
146 return true;
147 }
148 if (utils_1.isPlainObject(contents) || Array.isArray(contents)) {
149 if (exists) {
150 const existing = await fs_extra_1.default.readJSON(filePath);
151 if (JSON.stringify(existing) === JSON.stringify(contents)) {
152 return false;
153 }
154 }
155 if (options.log !== false) {
156 signale_1.default.debug(`wrote ${path_1.default.relative(getRootDir(), filePath)} JSON file`);
157 }
158 await fs_extra_1.default.writeJSON(filePath, contents, {
159 spaces: 4,
160 });
161 return true;
162 }
163 throw new Error('Invalid contents given to writeIfChanged');
164}
165exports.writeIfChanged = writeIfChanged;
166function formatList(list) {
167 return `\n - ${list.join('\n - ')}`;
168}
169exports.formatList = formatList;
170function writeHeader(msg, prefixNewline) {
171 if (prefixNewline)
172 process.stderr.write('\n');
173 signale_1.default.star(`${msg}`);
174}
175exports.writeHeader = writeHeader;
176function writePkgHeader(prefix, pkgInfos, prefixNewline) {
177 const names = pkgInfos.map(({ name }) => name).join(', ');
178 writeHeader(`${prefix} for ${names}`, prefixNewline);
179}
180exports.writePkgHeader = writePkgHeader;
181//# sourceMappingURL=misc.js.map
\No newline at end of file