This repository was archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapps.js
More file actions
127 lines (101 loc) · 3 KB
/
Copy pathapps.js
File metadata and controls
127 lines (101 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// @see https://github.com/npm/node-semver
const semver = require('semver');
// @see https://github.com/sindresorhus/global-dirs
const globalDirs = require('global-dirs');
// @see https://github.com/sindresorhus/is-path-inside
const isPathInside = require('is-path-inside');
// Tries to import a given module from the current working directory,
// or fallback to the global scope.
const importLocalOrGlobal = (module) => {
try {
const resolved = require.resolve(module, {
paths: [
process.cwd(),
globalDirs.npm.packages
]
});
return {
imported: require(resolved),
isGlobal: isPathInside(resolved, process.cwd()) === false
};
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
return {
imported: null,
isGlobal: true
};
}
throw e;
}
};
// Apps supported by our CLI
const supported = Object.entries({
// @see https://github.com/gridonic/webpack
'@gridonic/webpack': '>=0.4.0',
// @see https://github.com/gridonic/generator
'@gridonic/generator': '>=0.1.0',
}).map(([module, version]) => {
const blank = {
name: module,
version: null,
commands: {},
flags: {},
status: null,
isGlobal: false
};
const { imported, isGlobal } = importLocalOrGlobal(module);
// Module was not installed locally or globally
if (imported === null) {
const status = 'not installed';
return { ...blank, status };
}
const { cli = { version: null } } = imported;
// Module was found but is version is not supported by our CLI
if (semver.satisfies(cli.version, version) === false) {
let status = `not supported, expected ${version} but found `;
// Given app was supported once since it’s returning a version.
// Support has expired though.
if (cli.version !== null) {
status += cli.version;
} else {
status += 'very old version';
}
return { ...blank, status };
}
return {
...blank,
...cli,
isGlobal
};
});
// Collects all flags from all supported apps
const flags = ((apps) => {
const result = {};
apps.forEach(
({ flags }) => Object.entries(flags).forEach(
([key, value]) => result[key] = value
)
);
return result;
})(supported);
const findByCommand = command => supported.find(
search => Object.keys(search.commands).find(key => key === command) !== undefined
);
// Tries to run command by one of our supported apps
const run = (command, args = [], flags = {}) => {
const app = findByCommand(command);
if (app === undefined) {
return;
}
// Always print Gridonic CLI before running any app
console.log(
require('./brand')
);
app.commands[command].fn(args, flags);
};
module.exports = {
findByCommand,
supported,
flags,
run
};