-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuild.js
142 lines (116 loc) · 2.95 KB
/
build.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
#!/usr/bin/env node
/* Dependencies */
const fs = require('fs');
const pkg = require('./package.json');
const execNode = require('child_process').exec;
/* Helpers */
const exec = async (cmd) => {
return new Promise((resolve, reject) => {
execNode(cmd, (err, stdout, stderr) => {
if(err){
err.stdout = stdout;
err.stderr = stderr;
return reject(err);
}
if(stderr && !stderr.match(/ExperimentalWarning/)){
err = new Error(`Command failed: ${cmd}`);
err.stdout = stdout;
err.stderr = stderr;
return reject(err);
}
resolve(stdout);
});
});
};
const formatPerson = (person) => {
if(typeof(person) === 'string'){
return person;
}
const parts = [];
if(person.name){
parts.push(person.name);
}
if(person.email){
parts.push(`<${person.email}>`);
}
if(person.url){
parts.push(`(${person.url})`);
}
return parts.join(' ');
};
const readFile = async (path) => {
return new Promise((resolve, reject) => {
fs.readFile(path, (err, buffer) => {
if(err){
return reject(err);
}
resolve(buffer);
});
});
};
const writeFile = async (path, data) => {
return new Promise((resolve, reject) => {
fs.writeFile(path, data, (err) => {
if(err){
return reject(err);
}
resolve();
});
});
};
/* Build */
(async () => {
try {
const mainFilename = pkg.name;
console.log('Compiling TypeScript for Node...');
await exec('npx tsc');
console.log('Loading Source...');
const source = (await readFile(`./dist/${mainFilename}.js`)).toString().trim();
console.log('Loading License...');
const license = (await readFile('./LICENSE')).toString().trim();
console.log('Prepending Build and Project Information...');
const projectInfo = [
'/*!',
` * Project Name: ${pkg.name}`,
` * Project Description: ${pkg.description}`,
` * Version: ${pkg.version}`,
` * Build Timestamp: ${new Date().toISOString()}`,
` * Project Homepage: ${pkg.homepage}`,
` * Git Location: ${pkg.repository.url}`,
` * Authored By: ${formatPerson(pkg.author)}`,
pkg.maintainers && pkg.maintainers.length > 0 ? [
' * Maintained By:',
pkg.maintainers.map((maintainer) => {
return ` * ${formatPerson(maintainer)}`;
}).join('\n'),
].join('\n') : '',
pkg.contributors && pkg.contributors.length > 0 ? [
' * Contributors:',
pkg.contributors.map((contributor) => {
return ` * ${formatPerson(contributor)}`;
}).join('\n'),
].join('\n') : '',
` * License: ${pkg.license}`,
'*/',
license.split('\n').map((line, i, lines) => {
line = ' * ' + line;
if(i === 0){
line = '\n/*!\n' + line;
}else
if(i === lines.length - 1){
line += '\n*/';
}
return line;
}).join('\n').trim()
].filter((val) => {
return !!val;
}).join('\n').trim();
await writeFile(`./dist/${mainFilename}.js`, [
projectInfo,
source.trim()
].join('\n'));
console.log('Done building.');
}catch(err){
console.error(err);
}
})();