-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
404 lines (347 loc) · 12.9 KB
/
index.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* eslint-disable no-invalid-this */
"use strict";
// Require Node.js Dependencies
const assert = require("assert").strict;
const { readdir, readFile, stat } = require("fs").promises;
const { join, relative } = require("path");
const { pathToFileURL } = require("url");
// Require Third-party Dependencies
const parser = require("file-ignore-parser");
const Manifest = require("@slimio/manifest");
const globby = require("globby");
const is = require("@slimio/is");
const { yellow, gray, white } = require("kleur");
// Require Internal Dependencies
const requiredElem = require("./src/requiredElems.json");
const msg = require("./src/messages.js");
const checkFileContent = require("./src/files");
const { getJavascriptFiles } = require("./src/utils.js");
const { parseScript } = require("./src/ast.js");
const { WARN, CRIT, INFO } = require("./src/severities");
// CONSTANTS
const REQUIRE_DIR = requiredElem.REQUIRE_DIR;
const EXCLUDE_FILES = new Set(requiredElem.EXCLUDE_FILES);
const EXCLUDE_DIRS = new Set(requiredElem.EXCLUDE_DIRS);
const NAPI_DEPENDENCIES = new Set(["node-gyp-build", "node-addon-api"]);
const DIR_NAPI_EXCLUDE = new Set(["include", "prebuilds", "build"]);
const DIR_SERVICE_EXCLUDE = new Set(["scripts", "public", "views"]);
const STR = "\n ";
/**
* @function logHandler
* @description Log infos customs into the console
* @param {!string} severity emoji with const requiredElem.E_SEV
* @param {!string} message message MSG module
* @param {string} file file name
* @returns {void} Into the console
*/
function logHandler(severity, message, file) {
const colorFileName = white().bold(file);
if (severity === INFO && !this.showInformation) {
return;
}
if (severity === CRIT) {
this.count.crit++;
}
else if (severity === WARN) {
this.count.warn++;
}
// Messages into console
if (this.verbose) {
const isObject = is.plainObject(message);
let localMessage = isObject ? message.message : message;
localMessage = is.array(localMessage) ? localMessage.join(STR) : localMessage;
if (file === undefined) {
console.log(` ${severity} ${localMessage}`);
}
else {
console.log(` ${severity} ${colorFileName} ${localMessage}`);
}
if ((this.showDescription || severity === CRIT) && isObject && Reflect.has(message, "description")) {
console.log(message.description);
}
}
// Exit if case critical
if (severity === CRIT && !this.forceMode) {
process.exit(1);
}
}
/**
* @async
* @function psp
* @param {object} options options
* @param {boolean} [options.forceMode=false] enable forceMode
* @param {string} [options.CWD=process.cwd()] working dir where we will execute psp!
* @param {boolean} [options.isCLI=true] enable/disable CLI mode
* @param {boolean} [options.verbose=true] enable/disable verbose mode
* @param {boolean} [options.showDescription=false] enable/disable description of warnings
* @param {boolean} [options.showInformation=false] show information warnings!
* @returns {Promise<void>} Into the console with function log
*/
async function psp(options = Object.create(null)) {
const {
forceMode = false,
CWD = process.cwd(),
isCLI = true,
verbose = true,
showDescription = false,
showInformation = false
} = options;
const ctx = {
forceMode, count: { crit: 0, warn: 0 }, typeOfProject: "", CWD, verbose, showDescription, showInformation
};
const log = logHandler.bind(ctx);
if (verbose) {
console.log(gray().bold(`\n > Running Project Struct Policy at ${yellow().bold(CWD)}\n`));
}
// Read the main directory of user
const elemMainDir = new Set(await readdir(CWD));
// If slimio manisfest doesn't installed in this project, then exit
if (!elemMainDir.has("slimio.toml")) {
log(CRIT, msg.manifest);
if (isCLI) {
process.exit(1);
}
}
let pkg;
try {
const str = await readFile(join(CWD, "package.json"));
pkg = JSON.parse(str);
}
catch (error) {
log(CRIT, error.message, "package.json");
return ctx.count;
}
const pkgHasWhiteList = Reflect.has(pkg, "files");
if (pkgHasWhiteList && Array.isArray(pkg.files)) {
if (elemMainDir.has(".npmignore")) {
log(CRIT, msg.duplicatedWhiteList, ".npmignore");
}
const matchingFiles = await globby(pkg.files, { cwd: CWD });
for (const path of pkg.files) {
if (path.includes("*")) {
continue;
}
const someMatch = matchingFiles.some((completeName) => completeName.startsWith(path));
if (!someMatch) {
log(CRIT, msg.pubNoMatch(path));
}
}
}
// If type of .toml file isn't valid
const manifest = Manifest.open(join(CWD, "slimio.toml"));
ctx.typeOfProject = manifest.type.toLowerCase();
ctx.platform = manifest.platform;
ctx.psp = manifest.psp;
// Check version of package/slimio
if (pkg.version !== manifest.version) {
log(CRIT, msg.versionDiff);
}
// If slimio.toml exists for projects structure
switch (ctx.typeOfProject) {
case "service": {
if (!elemMainDir.has(".env")) {
log(CRIT, msg.env, ".env");
}
const devDep = pkg.devDependencies || {};
const dep = pkg.dependencies || {};
if (!Reflect.has(devDep, "dotenv") && !Reflect.has(dep, "dotenv")) {
log(WARN, msg.dotenv, "package.json");
}
break;
}
case "cli": {
if (!elemMainDir.has("bin")) {
log(CRIT, msg.binNotExist);
}
try {
const ctnIndexFile = await readFile(join(CWD, "bin", "index.js"), { encoding: "utf8" });
if (!ctnIndexFile.includes("#!/usr/bin/env node")) {
log(WARN, msg.shebang);
break;
}
}
catch (error) {
log(CRIT, msg.indexJsNotExist);
}
if (Reflect.has(pkg, "preferGlobal")) {
log(WARN, msg.cliGlobal);
}
if (typeof pkg.bin !== "object") {
log(WARN, msg.rootFieldsCLI);
}
break;
}
case "napi": {
// If include folder doesn't exist.
if (!elemMainDir.has("include")) {
log(CRIT, msg.gyp.missingIncludeDir);
}
// If binding.gyp file doesn't exist
if (!elemMainDir.has("binding.gyp")) {
log(CRIT, msg.gyp.missingGyp, "binding.gyp");
}
// Infos: gypfile in package.json
if (!Reflect.has(pkg, "gypfile")) {
log(WARN, msg.gyp.missingRootGypProperty, "package.json");
}
break;
}
case "addon": {
let addon = null;
try {
const main = pkg.main || "index.js";
if (pkg.type === "module") {
const addonEntryFile = pathToFileURL(join(CWD, main));
addon = (await import(addonEntryFile)).default;
}
else {
// eslint-disable-next-line
addon = require(join(CWD, main));
}
assert.strictEqual(addon.constructor.name, "Addon");
if (addon.name !== manifest.name) {
log(CRIT, msg.nameDiff);
}
}
catch (err) {
log(CRIT, msg.exportAddon);
}
}
}
// Loop on required files array
const skipFiles = new Set(["index.d.ts", ".npmrc", ".travis.yml", ".eslintignore"]);
if (!manifest.psp.jsdoc) {
skipFiles.add("jsdoc.json");
}
const skipDegraded = new Set([".eslintrc", "jsdoc.json"]);
const skipTypes = new Set(["addon", "cli", "service"]);
for (let fileName of requiredElem.FILE_TO_CHECKS) {
const hasLowerCaseFile = elemMainDir.has(fileName.toLowerCase());
if (hasLowerCaseFile) {
fileName = fileName.toLowerCase();
}
if (!hasLowerCaseFile && !elemMainDir.has(fileName)) {
// If type === addon
const isAddonOrCLI = skipTypes.has(ctx.typeOfProject);
if (fileName === "index.d.ts" && isAddonOrCLI) {
continue;
}
if (fileName === "binding.gyp" && ctx.typeOfProject !== "napi") {
continue;
}
if (ctx.typeOfProject === "degraded" && skipDegraded.has(fileName)) {
continue;
}
// If file doesn't exist
if (skipFiles.has(fileName)) {
if (fileName === ".npmrc" && !ctx.psp.npmrc) {
continue;
}
log(INFO, msg.fileNotExist, fileName);
continue;
}
if (fileName === "jsdoc.json" && isAddonOrCLI) {
log(WARN, msg.fileNotExist, fileName);
continue;
}
// Ignore npm ignore if we have a white list in package.json
if (fileName === ".npmignore" && pkgHasWhiteList) {
continue;
}
log(ctx.typeOfProject === "degraded" ? WARN : CRIT, msg.fileNotExist, fileName);
if (ctx.forceMode) {
continue;
}
}
if (!EXCLUDE_FILES.has(fileName)) {
await checkFileContent(fileName, logHandler.bind(ctx), ctx);
}
}
// Check for require statment
{
const tempDependenciesArray = [];
const isESM = Reflect.has(pkg, "type") && pkg.type === "module";
for await (const file of getJavascriptFiles(CWD)) {
const relativeFile = relative(CWD, file);
if (ctx.psp.exclude.some((path) => relativeFile.startsWith(path))) {
continue;
}
try {
const dep = await parseScript(file, { module: isESM });
tempDependenciesArray.push(...dep);
}
catch (err) {
if (verbose) {
console.error(`Failed to parse script ${file}:: ${err.message}`);
}
}
}
const runtimeDep = new Set(tempDependenciesArray);
const dependencies = pkg.dependencies || {};
const disabledDeps = new Set(ctx.psp.disabled_dependency);
for (const dep of runtimeDep) {
if (!Reflect.has(dependencies, dep) && !disabledDeps.has(dep)) {
log(WARN, msg.missingDep(dep), "package.json");
}
}
for (const dep of Object.keys(dependencies)) {
if (disabledDeps.has(dep) || runtimeDep.has(dep) ||
(ctx.typeOfProject === "napi" && NAPI_DEPENDENCIES.has(dep))) {
continue;
}
log(WARN, msg.unusedDep(dep), "package.json");
}
}
// Check directories
{
const missingDirectories = REQUIRE_DIR[0].filter((name) => !elemMainDir.has(name));
for (const dirName of missingDirectories) {
switch (dirName) {
case "test":
case "src":
if (ctx.typeOfProject !== "degraded") {
log(WARN, msg[dirName], dirName);
}
break;
default:
log(INFO, msg[dirName], dirName);
}
}
}
// If .gitignore doesn't exist, then we dont want the next section to execute!
if (!elemMainDir.has(".gitignore")) {
log(CRIT, msg.gitExist);
return ctx.count;
}
const requiredSets = new Set(REQUIRE_DIR[0]);
const ignoredSets = await Promise.all([
parser(join(CWD, ".gitignore")),
pkgHasWhiteList ? pkg.files : parser(join(CWD, ".npmignore"))
]);
const ignoredFiles = new Set([...ignoredSets[0], ...ignoredSets[1]].map((file) => relative(CWD, file)));
const ignoredDirs = [...ignoredFiles]
.filter((file) => !file.includes("*"))
.filter((file) => elemMainDir.has(file))
.filter((file) => !requiredSets.has(file))
.filter((file) => !EXCLUDE_DIRS.has(file));
for (const dir of ignoredDirs) {
if (manifest.psp.exclude.includes(dir)) {
continue;
}
if (ctx.typeOfProject === "napi" && DIR_NAPI_EXCLUDE.has(dir)) {
continue;
}
if (ctx.typeOfProject === "service" && DIR_SERVICE_EXCLUDE.has(dir)) {
continue;
}
if (dir === ".env" || (dir === "bin" && ctx.typeOfProject === "cli")) {
continue;
}
const st = await stat(join(CWD, dir));
if (st.isDirectory()) {
log(WARN, msg.ignoreDir, dir);
}
}
return ctx.count;
}
module.exports = psp;