-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-plugins.ts
230 lines (211 loc) Β· 6.48 KB
/
build-plugins.ts
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import esbuild from 'esbuild';
import { solidPlugin } from 'esbuild-plugin-solid';
import esbuildSvelte from 'esbuild-svelte';
import fs from 'fs';
import { copyFile, mkdir, readdir } from 'fs/promises';
import path from 'path';
import sveltePreprocess from 'svelte-preprocess';
const home = require('os').homedir();
const PLUGINS_INSTALL_DIR = path.join(home, '.yal', 'plugins');
// Remove the PLUGINS_DIR and remake it
if (fs.existsSync(PLUGINS_INSTALL_DIR)) {
fs.rmdirSync(PLUGINS_INSTALL_DIR, { recursive: true });
fs.mkdirSync(PLUGINS_INSTALL_DIR);
}
// remove the dist dir and remake it
if (fs.existsSync('./dist/')) {
fs.rmdirSync('./dist/', { recursive: true });
fs.mkdirSync('./dist/');
}
// if (!fs.existsSync('./dist/')) {
// fs.mkdirSync('./dist/');
// }
const PLUGINS_SRC_DIR = './plugins/';
function getEntryPoints({ includeSolidJS }: { includeSolidJS: boolean }) {
// get args from command line
const args = process.argv.slice(2);
// console.log({ args });
const userArgs = args.filter((x) => x !== '--watch');
const userHasSpecifiedPlugin = userArgs.length > 0;
// console.log(userHasSpecifiedPlugin);
// if there are userArgs, use them as entry points
if (userHasSpecifiedPlugin) {
console.log(
`User has specified plugins to build. Looking for these plugins: ${userArgs}}`
);
}
const arr = fs
.readdirSync(PLUGINS_SRC_DIR)
.filter((x) => x !== '.DS_Store')
.filter((x) => (userHasSpecifiedPlugin ? args.includes(x) : true))
.map((x) => path.resolve(process.cwd(), PLUGINS_SRC_DIR, x))
.filter((x) => {
const packageJson = fs.readFileSync(x + '/package.json', 'utf8');
const packageJsonParsed = JSON.parse(packageJson);
// console.log(packageJsonParsed.dependencies?.['solid-js']);
if (packageJsonParsed.dependencies?.['solid-js']) {
return includeSolidJS ? true : false;
}
return includeSolidJS ? false : true;
})
.map((path) => {
const contents = fs.readdirSync(path + '/src');
// console.log({ contents });
if (contents.includes('index.js')) {
return path + '/src/index.js';
}
if (contents.includes('index.ts')) {
return path + '/src/index.ts';
}
if (contents.includes('index.tsx')) {
return path + '/src/index.tsx';
}
if (contents.includes('index.jsx')) {
return path + '/src/index.jsx';
}
console.log(
'No index.js or index.ts or index.jsx or index.tsx found in ' + path
);
});
console.log({ arr });
return arr;
}
async function build({
pluginFiles,
includeSolidJS,
includeSvelte,
}: {
pluginFiles: string[];
includeSolidJS: boolean;
includeSvelte: boolean;
}) {
function getPlugins() {
if (includeSolidJS) {
return [solidPlugin()];
}
if (includeSvelte) {
return [
esbuildSvelte({
preprocess: sveltePreprocess(),
}),
];
}
return [];
}
console.log({ includeSolidJS, includeSvelte });
return esbuild
.build({
entryPoints: pluginFiles,
mainFields: ['svelte', 'browser', 'module', 'main'],
outdir: './dist/',
format: 'esm',
minify: false, // so the resulting code is easier to understand
bundle: true,
splitting: false,
allowOverwrite: true,
loader: {
'.png': 'dataurl',
'.jpg': 'dataurl',
'.svg': 'dataurl',
'.js': 'jsx',
'.ts': 'tsx',
'.tsx': 'tsx',
'.jsx': 'jsx',
},
sourcemap: 'external',
plugins: getPlugins(),
watch:
// if args contain --watch
process.argv.includes('--watch')
? {
onRebuild(error, result) {
if (error) {
console.error('watch build failed:', error);
} else {
console.log('watch build succeeded:', result);
copyDir('./dist', PLUGINS_INSTALL_DIR, true).then(() =>
console.log('Plugins installed π')
);
}
},
}
: false,
})
.catch((err) => {
console.error(err);
process.exit(1);
});
}
(async () => {
console.log('Building plugins...');
const pluginFiles = getEntryPoints({ includeSolidJS: false });
console.log('pluginFiles (No SolidJS dependency)', pluginFiles);
await build({ pluginFiles, includeSolidJS: false, includeSvelte: true });
console.log('Plugins compiled (no SolidJS) π');
const pluginFilesWithSolidJS = getEntryPoints({ includeSolidJS: true });
console.log('pluginFiles (SolidJS dependencies)', pluginFilesWithSolidJS);
await build({
pluginFiles: pluginFilesWithSolidJS,
includeSolidJS: true,
includeSvelte: false,
});
console.log('SolidJS Plugins compiled π');
await copyDir(PLUGINS_SRC_DIR, './dist/');
console.log('Other files copied π');
await copyDir('./dist', PLUGINS_INSTALL_DIR, true);
console.log('Plugins installed π');
copyYarnLockAndInstallDeps();
})();
async function copyDir(src, dest, copySrcDirectory = false) {
await mkdir(dest, { recursive: true });
let entries = await readdir(src, { withFileTypes: true });
for (let entry of entries) {
if (entry.name === '.DS_Store') continue;
if (entry.name === 'node_modules') continue;
if (entry.name === 'src' && !copySrcDirectory) continue;
let srcPath = path.join(src, entry.name);
let destPath = path.join(dest, entry.name);
entry.isDirectory()
? await copyDir(srcPath, destPath, copySrcDirectory)
: await copyFile(srcPath, destPath);
}
}
function copyYarnLockAndInstallDeps() {
// const src = path.resolve(process.cwd(), './yarn.lock');
// const dest = path.resolve(process.cwd(), `${PLUGINS_INSTALL_DIR}/yarn.lock`);
// try {
// fs.copyFileSync(src, dest);
// } catch (err) {
// console.log(err);
// console.log(`maybe have been an error copying: ${src} to ${dest}`);
// }
const packageJsonTemplate = `
{
"name": "@package/plugins",
"version": "0.0.1",
"workspaces": {
"packages": [
"plugins/*"
]
},
"private": true
}`;
// Create package.json
fs.writeFileSync(
path.resolve(process.cwd(), `${PLUGINS_INSTALL_DIR}/package.json`),
packageJsonTemplate
);
// Install dependencies
const { exec } = require('child_process');
exec(
'yarn install',
{ cwd: path.join(home, '.yal') },
(err, stdout, stderr) => {
if (err) {
console.log(err);
return;
}
console.log(stdout);
}
);
}