-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathpackage.js
More file actions
157 lines (135 loc) · 4.74 KB
/
package.js
File metadata and controls
157 lines (135 loc) · 4.74 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
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
//
// Copyright (c) Microsoft.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
// This is basically a cheap Makefile. In JavaScript. Because...
const { readdir, stat, mkdir, copyFile, unlink, rmdir, writeFile } = require('fs').promises;
const path = require('path');
const templateSpecificExclusions = require('../projections/common-excludes.json');
const projectionsPath = path.resolve(path.join(__dirname, '..', 'projections'));
const sharedPath = path.resolve(path.join(__dirname, '..', 'shared'));
const templatesPath = path.resolve(path.join(__dirname, '..', 'templates'));
function logWithFooter(msg) {
console.log(msg);
console.log('='.repeat(msg.length));
}
const appPackage = require('../package.json');
const publishingPackage = path.resolve('__dirname', '..' ,'publishing-package.json');
async function build() {
if (await exists(templatesPath)) {
console.log(`Cleaning up existing templates from ${templatesPath}`);
await burn(templatesPath);
await rmdir(templatesPath, { recursive: true });
console.log();
}
await mkdir(templatesPath);
// Create a packaging version
appPackage.private = false;
console.log(`Writing ${publishingPackage}`);
await writeFile(publishingPackage, JSON.stringify(appPackage, null, 2));
console.log();
const sharedFiles = await allFiles(sharedPath);
logWithFooter(`Shared common files from ${sharedPath}`);
sharedFiles.map(file => console.log(file));
console.log();
const templateNames = await directoryNames(projectionsPath);
for (const templateName of templateNames) {
await tryBuildTemplate(projectionsPath, templateName, sharedFiles);
}
const definitionsFile = 'definitions.json';
await copyTo(`- ${definitionsFile}`, path.join(projectionsPath, definitionsFile), path.join(templatesPath, definitionsFile));
}
async function tryBuildTemplate(projectionsPath, templateName, sharedFiles) {
logWithFooter(templateName);
const templateSourceRoot = path.join(projectionsPath, templateName);
const destinationRoot = path.join(templatesPath, templateName);
if (await exists(destinationRoot)) {
throw new Error(`Destination root ${destinationRoot} for template ${templateName} already exists...`);
}
await mkdir(destinationRoot);
const specialExclusions = templateSpecificExclusions[templateName];
if (specialExclusions) {
console.log('This template excludes some shared, common files, if present:');
console.dir(specialExclusions);
console.log();
}
for (const file of sharedFiles) {
if (specialExclusions && specialExclusions.includes(file.toLowerCase())) {
console.log(`- ${file} (excluded)`);
continue;
}
await copyTo(`+ ${file}`, path.join(sharedPath, file), path.join(destinationRoot, file));
}
const customTemplateFiles = await allFiles(templateSourceRoot);
if (customTemplateFiles.length) {
console.log();
console.log(`Placing ${customTemplateFiles.length} template-specific files...`);
for (const file of customTemplateFiles) {
await copyTo(`+ ${file}`, path.join(templateSourceRoot, file), path.join(destinationRoot, file));
}
}
console.log();
}
build().then(exit => process.exit(0)).catch(error => {
console.log(error);
process.exit(1);
});
async function burn(folder) {
const files = await allFiles(folder);
for (let i = 0; i < files.length; i++) {
const p = path.join(folder, files[i]);
console.log(`Burning ${p}`)
await unlink(p);
}
}
async function copyTo(shortName, source, dest) {
console.log(shortName);
const dirname = path.dirname(dest);
if (!await exists(dirname)) {
console.log(`Creating destination directory ${dirname}`);
await mkdir(dirname);
}
console.log(` ${source} -> ${dest}`);
await copyFile(source, dest);
}
async function directoryNames(dir) {
const dirs = [];
for (const file of await readdir(dir)) {
if ((await stat(path.join(dir, file))).isDirectory()) {
dirs.push(file);
}
}
return dirs;
}
async function allFiles(rootDirectory, subPath) {
let files = [];
subPath = subPath ? `${subPath}/` : '';
if (!rootDirectory) {
console.log(rootDirectory);
console.log(subPath);
throw new Error('wtf');
}
if (rootDirectory) {
for (const file of await readdir(rootDirectory)) {
const joined = path.join(rootDirectory, file);
const statObject = await stat(joined);
if (statObject.isDirectory()) {
files = [...files, ...await allFiles(joined, `${subPath}${file}`)];
} else if (statObject.isFile()) {
files.push(`${subPath}${file}`);
}
}
}
return files;
}
async function exists(p) {
try {
await stat(p);
return true;
} catch (error) {
if (error.code === 'ENOENT') {
return false;
}
throw error;
}
}