forked from tflanagan/node-quickbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
206 lines (169 loc) · 4.33 KB
/
build.js
File metadata and controls
206 lines (169 loc) · 4.33 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
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
#!/usr/bin/env node
/* Dependencies */
const fs = require('fs');
const pkg = require('./package.json');
const minify = require('minify');
const execNode = require('child_process').exec;
const Browserify = require('browserify');
const { transpileModule } = require('typescript');
/* Helpers */
const browserify = async (files, options) => {
return new Promise((resolve, reject) => {
const b = new Browserify(files, options);
b.bundle((err, src) => {
if(err){
return reject(err);
}
resolve(src);
});
});
};
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 unlinkFile = async (path) => {
return new Promise((resolve, reject) => {
fs.unlink(path, (err) => {
if(err){
return reject(err);
}
resolve();
});
});
};
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('Browserify...');
const browserifiedPrep = await browserify([
`./dist/${mainFilename}.js`
]);
console.log('Compiling for Browser...');
const browserified = transpileModule(browserifiedPrep.toString(), {
compilerOptions: {
target: 'ES5',
module: 'commonjs',
lib: [
'dom',
'ES6'
],
allowJs: true,
checkJs: false,
sourceMap: false,
declaration: false,
removeComments: true
}
});
await writeFile(`./dist/${mainFilename}.browserify.js`, browserified.outputText);
console.log('Minify Browser...');
const browserSource = await minify(`./dist/${mainFilename}.browserify.js`);
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}.browserify.min.js`, [
projectInfo,
browserSource.trim()
].join('\n'));
await writeFile(`./dist/${mainFilename}.js`, [
projectInfo,
source.trim()
].join('\n'));
console.log('Cleanup...');
await unlinkFile(`./dist/${mainFilename}.browserify.js`);
console.log('Done building.');
}catch(err){
console.error(err);
}
})();