forked from windycom/windy-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.js
executable file
·211 lines (166 loc) · 6.15 KB
/
compiler.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
#!/usr/bin/env node --no-warnings
/**
* This is plugin building script. Feel free to modify it
* All is MIT licenced
*/
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import assert from 'node:assert';
import https from 'node:https';
import { program } from 'commander';
import prompts from 'prompts';
import ucfirst from 'ucfirst';
import c from 'consola';
import { yellow, gray } from 'colorette';
import express from 'express';
import chokidar from 'chokidar';
import decache from 'decache';
import { builder } from './dev/rollup.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const port = 9999;
const { version, name, author, repository, description } = JSON.parse(
fs.readFileSync(path.join(__dirname, 'package.json')),
);
const prog = program
.option('-b, --build', 'Build the plugin in required directory (default src)')
.option('-w, --watch', 'Build plugin and watch file changes in required directory')
.option('-s, --serve', `Serve dist directory on port ${port}`)
.option('-p, --prompt', 'Show command line promt with all the examples')
.option('-t, --transpile', 'Transpile your code with Babel')
.parse(process.argv)
.opts();
console.log(prog);
if (!process.argv.slice(2).length) {
program.outputHelp();
process.exit();
}
let config,
srcDir = 'src';
export const prompt = async () => {
let dir = 'src';
const list = fs.readdirSync(path.join(__dirname, 'examples')).filter(d => /\d\d-/.test(d));
console.log(`\nSelect which example you want to test:\n`);
list.map((d, i) =>
console.log(` ${yellow(i + 1)}) ${ucfirst(d.replace(/^\d\d-/, '').replace(/-/g, ' '))}`),
);
console.log(`\n ${yellow(0)}) I want to develop ${yellow('my own plugin')}.\n`);
const { value } = await prompts({
type: 'number',
name: 'value',
message: `Which example you want to launch? (press 0 - ${list.length}):`,
validate: value => Boolean(value >= 0 && value < list.length + 1),
});
if (value > 0) {
dir = path.join('examples', list[value - 1]);
} else if (value === 0) {
console.log(`----------------------------------------------------
Please change ${yellow('package.json')} now:
${yellow('name')}: Must contain name of your plugin in a form windy-plugin-AnyName
${yellow('description')}: Should be description of what your plugin does
${yellow('author')}: Should contain your name
${yellow('repository')}: Should be actual link to your hosting repo
Also ${yellow('./README.md')} should contain some info about your plugin if you wish
For faster work use directlly ${yellow('npm run start-dev')} to skip this prompt
After you will be done use ${yellow('npm publish')} to publish your plugin.
-----------------------------------------------------`);
}
return dir;
};
// Main
(async () => {
if (!fs.existsSync(path.join(__dirname, 'dist'))) {
fs.mkdirSync(path.join(__dirname, 'dist'));
}
console.log(`\nBuilding ${yellow(name)}, version ${yellow(version)}`);
// Beginners example selection
if (prog.prompt) {
srcDir = await prompt();
}
c.info(`Compiler will compile ${yellow(`./${srcDir}/`)}`);
await reloadConfig();
try {
// Basic assertions
assert(
typeof config === 'object',
'Missing basic config object. Make sure you have valid ' + 'config.js in src dir',
);
assert(
/^windy-plugin-/.test(name),
'Your repository (and also your published npm package) ' +
'must be named "windy-plugin-AnyOfYourName".' +
' Change the name in your package.json',
);
// Tasks
if (prog.watch || prog.build) {
await build();
}
if (prog.serve) {
await startServer();
}
if (prog.watch) {
c.start(
`Staring watch on ${gray(srcDir)} and ${gray(
'package.json',
)}. Build 1 sec after change....`,
);
chokidar
.watch([srcDir], {
awaitWriteFinish: { stabilityThreshold: 1000, pollInterval: 100 },
})
.on('change', onChange);
}
} catch (e) {
c.error(`Error\u0007`, e);
}
})();
function startServer() {
return new Promise(resolve => {
const httpsOptions = {
// https://www.ibm.com/support/knowledgecenter/en/SSWHYP_4.0.0/com.ibm.apimgmt.cmc.doc/task_apionprem_gernerate_self_signed_openSSL.html
key: fs.readFileSync(path.join(__dirname, 'dev', 'key.pem'), 'utf8'),
cert: fs.readFileSync(path.join(__dirname, 'dev', 'certificate.pem'), 'utf8'),
};
app.use(express.static('dist'));
https.createServer(httpsOptions, app).listen(port, () => {
c.success(
`Your plugin is published at
${gray('https://localhost:' + port + '/plugin.js')}.
Use ${yellow('https://www.windy.com/dev')} to test it.\n`,
);
resolve();
});
});
}
/* This is main build function
Feel free to to use your own builder, transpiler, minifier or whatever
The result must be a single .js file with single W.loadPlugin() function
Make sure to replace import XY from '@windy/XY' with W.require(XY)
*/
async function build() {
const destination = path.join(__dirname, 'dist');
const meta = {
name,
version,
author,
repository,
description,
...config,
};
const { code } = await builder(name, srcDir, meta);
fs.writeFileSync(path.join(destination, 'plugin.js'), code);
c.success(`Your plugin ${gray(name)} has been compiled to ${gray(destination)}`);
}
async function reloadConfig() {
const { default: dir } = await import(path.join(__dirname, srcDir, 'config.js'));
decache(dir);
config = dir;
return;
}
async function onChange(fullPath) {
c.info(`watch: File changed ${gray(fullPath)}`);
await reloadConfig();
await build();
}