UNPKG

2.47 kBJavaScriptView Raw
1"use strict";
2
3// @ts-check
4const fs = require("fs");
5
6const path = require("path");
7
8const arrify = require("arrify");
9
10const has = require("lodash.has");
11
12const readPkgUp = require("read-pkg-up");
13
14const which = require("which");
15
16const glob = require("glob");
17
18const chalk = require("chalk").default;
19
20const {
21 testMatch,
22 testIgnores
23} = require("./config/jest.patterns");
24
25const {
26 pkg,
27 path: pkgPath
28} = readPkgUp.sync({
29 cwd: fs.realpathSync(process.cwd())
30});
31const appDirectory = path.dirname(pkgPath);
32
33const isGdScripts = () => pkg.name === "gd-scripts";
34/**
35 * @param {string} modName
36 * @param {{ executable?: string, cwd?: string }} options
37 */
38
39
40const resolveBin = (modName, {
41 executable = modName,
42 cwd = process.cwd()
43} = {}) => {
44 let pathFromWhich;
45
46 try {
47 pathFromWhich = fs.realpathSync(which.sync(executable));
48 } catch (_error) {// ignore _error
49 }
50
51 try {
52 const modPkgPath = require.resolve(`${modName}/package.json`);
53
54 const modPkgDir = path.dirname(modPkgPath);
55
56 const {
57 bin
58 } = require(modPkgPath);
59
60 const binPath = typeof bin === "string" ? bin : bin[executable];
61 const fullPathToBin = path.join(modPkgDir, binPath);
62
63 if (fullPathToBin === pathFromWhich) {
64 return executable;
65 }
66
67 return fullPathToBin.replace(cwd, ".");
68 } catch (error) {
69 if (pathFromWhich) {
70 return executable;
71 }
72
73 throw error;
74 }
75};
76/**
77 * @param {string[]} p
78 */
79
80
81const fromRoot = (...p) => path.join(appDirectory, ...p);
82/**
83 * @param {string[]} p
84 */
85
86
87const hasFile = (...p) => fs.existsSync(fromRoot(...p));
88/**
89 * @param {any} props
90 */
91
92
93const hasPkgProp = props => arrify(props).some(prop => has(pkg, prop));
94
95const hasTests = () => {
96 const testPatterns = testMatch.join(",");
97 const ignorePatterns = testIgnores.map(x => `${x}${x.endsWith("/") ? "" : "/"}**/*`);
98 const testList = glob.sync(`{${testPatterns}}`, {
99 ignore: ignorePatterns
100 });
101 return testList.length > 0;
102};
103/**
104 * @param {string} message
105 */
106
107
108const logMessage = message => {
109 console.log(`${chalk.bgCyan(chalk.black("[gd-scripts]"))} ${message}\n`);
110};
111/**
112 * @param {string} script
113 */
114
115
116const logScriptMessage = script => {
117 const scriptMessage = `Running ${chalk.cyan(script.toUpperCase())} script.`;
118 logMessage(scriptMessage);
119};
120
121module.exports = {
122 resolveBin,
123 fromRoot,
124 hasFile,
125 hasPkgProp,
126 isGdScripts,
127 hasTests,
128 logMessage,
129 logScriptMessage
130};
131//# sourceMappingURL=utils.js.map
\No newline at end of file