forked from h5bp/html5-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_existence.mjs
127 lines (96 loc) · 2.93 KB
/
file_existence.mjs
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
import assert from 'assert';
import fs from 'fs';
import path from 'path';
import glob from 'glob';
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const pkg = require('../package.json');
const dirs = pkg['h5bp-configs'].directories;
const expectedFilesInArchiveDir = [
`${pkg.name}_v${pkg.version}.zip`
];
const expectedFilesInDistDir = [
'.editorconfig',
'.gitattributes',
'.gitignore',
'404.html',
'package.json',
'webpack.config.js',
'css/', // for directories, a `/` character
// should be included at the end
'css/normalize.css',
'css/style.css',
'favicon.ico',
'icon.png',
'icon.svg',
'img/',
'img/.gitignore',
'index.html',
'js/',
'js/app.js',
'js/vendor/',
`js/vendor/modernizr-${pkg.devDependencies.modernizr}.min.js`,
'LICENSE.txt',
'robots.txt',
'site.webmanifest',
'tile-wide.png',
'tile.png'
];
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function checkFiles(directory, expectedFiles) {
// Get the list of files from the specified directory
const files = glob.sync('**/*', {
'cwd': directory,
'ignore': [
'**/node_modules/**',
'package-lock.json',
'**/dist/**',
'**/.cache/**',
],
'dot': true, // include hidden files
'mark': true // add a `/` character to directory matches
});
// Check if all expected files are present in the
// specified directory, and are of the expected type
expectedFiles.forEach((file) => {
let ok = false;
const expectedFileType = (file.slice(-1) !== '/' ? 'regular file' : 'directory');
// If file exists
if (files.indexOf(file) !== -1) {
// Check if the file is of the correct type
if (file.slice(-1) !== '/') {
// Check if the file is really a regular file
ok = fs.statSync(path.resolve(directory, file)).isFile();
} else {
// Check if the file is a directory
// (Since glob adds the `/` character to directory matches,
// we can simply check if the `/` character is present)
ok = (files[files.indexOf(file)].slice(-1) === '/');
}
}
it(`"${file}" should be present and it should be a ${expectedFileType}`, () =>{
assert.equal(true, ok);
});
});
// List all files that should be NOT
// be present in the specified directory
(files.filter((file) => {
return expectedFiles.indexOf(file) === -1;
})).forEach((file) => {
it(`"${file}" should NOT be present`, () => {
assert(false);
});
});
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function runTests() {
describe('Test if all the expected files, and only them, are present in the build directories', () => {
describe(dirs.archive, () => {
checkFiles(dirs.archive, expectedFilesInArchiveDir);
});
describe(dirs.dist, () => {
checkFiles(dirs.dist, expectedFilesInDistDir);
});
});
}
runTests();