1 | "use strict";
|
2 |
|
3 |
|
4 | const fs = require("fs");
|
5 |
|
6 | const path = require("path");
|
7 |
|
8 | const arrify = require("arrify");
|
9 |
|
10 | const has = require("lodash.has");
|
11 |
|
12 | const readPkgUp = require("read-pkg-up");
|
13 |
|
14 | const which = require("which");
|
15 |
|
16 | const glob = require("glob");
|
17 |
|
18 | const chalk = require("chalk").default;
|
19 |
|
20 | const {
|
21 | testMatch,
|
22 | testIgnores
|
23 | } = require("./config/jest.patterns");
|
24 |
|
25 | const {
|
26 | pkg,
|
27 | path: pkgPath
|
28 | } = readPkgUp.sync({
|
29 | cwd: fs.realpathSync(process.cwd())
|
30 | });
|
31 | const appDirectory = path.dirname(pkgPath);
|
32 |
|
33 | const isGdScripts = () => pkg.name === "gd-scripts";
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | const 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) {
|
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 |
|
78 |
|
79 |
|
80 |
|
81 | const fromRoot = (...p) => path.join(appDirectory, ...p);
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | const hasFile = (...p) => fs.existsSync(fromRoot(...p));
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | const hasPkgProp = props => arrify(props).some(prop => has(pkg, prop));
|
94 |
|
95 | const 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 |
|
105 |
|
106 |
|
107 |
|
108 | const logMessage = message => {
|
109 | console.log(`${chalk.bgCyan(chalk.black("[gd-scripts]"))} ${message}\n`);
|
110 | };
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | const logScriptMessage = script => {
|
117 | const scriptMessage = `Running ${chalk.cyan(script.toUpperCase())} script.`;
|
118 | logMessage(scriptMessage);
|
119 | };
|
120 |
|
121 | module.exports = {
|
122 | resolveBin,
|
123 | fromRoot,
|
124 | hasFile,
|
125 | hasPkgProp,
|
126 | isGdScripts,
|
127 | hasTests,
|
128 | logMessage,
|
129 | logScriptMessage
|
130 | };
|
131 |
|
\ | No newline at end of file |