|
| 1 | +import { watch } from 'chokidar'; |
| 2 | +import * as esbuild from 'esbuild'; |
| 3 | +import { execa } from 'execa'; |
| 4 | +import fs from 'node:fs'; |
| 5 | +import { createRequire } from 'node:module'; |
| 6 | +import { join, dirname } from 'path'; |
| 7 | +import { Worker } from 'node:worker_threads'; |
| 8 | +import { fileURLToPath } from 'node:url'; |
| 9 | + |
| 10 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 11 | +const require = createRequire(import.meta.url); |
| 12 | +const production = process.argv.includes('--production'); |
| 13 | +const isWatch = process.argv.includes('--watch'); |
| 14 | + |
| 15 | +async function main() { |
| 16 | + const ctx = await esbuild.context({ |
| 17 | + entryPoints: { |
| 18 | + extension: 'src/extension.ts', |
| 19 | + server: './src/language-server/index.ts', |
| 20 | + }, |
| 21 | + bundle: true, |
| 22 | + format: 'cjs', |
| 23 | + minify: production, |
| 24 | + sourcemap: !production, |
| 25 | + sourcesContent: false, |
| 26 | + tsconfig: './tsconfig.json', |
| 27 | + platform: 'node', |
| 28 | + outdir: 'dist', |
| 29 | + define: { 'process.env.NODE_ENV': production ? '"production"' : '"development"' }, |
| 30 | + external: ['vscode'], |
| 31 | + plugins: [esbuildUMD2ESMPlugin], |
| 32 | + }); |
| 33 | + |
| 34 | + if (isWatch) { |
| 35 | + const buildMetadataSchemaDebounced = debounce(buildMetadataSchema, 100); |
| 36 | + const dependencyPath = dirname(require.resolve('@tutorialkit/types')); |
| 37 | + |
| 38 | + watch(dependencyPath).on('all', (eventName, path) => { |
| 39 | + if (eventName !== 'change' && eventName !== 'add' && eventName !== 'unlink') { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + buildMetadataSchemaDebounced(); |
| 44 | + }); |
| 45 | + |
| 46 | + await Promise.all([ |
| 47 | + ctx.watch(), |
| 48 | + execa('tsc', ['--noEmit', '--watch', '--preserveWatchOutput', '--project', 'tsconfig.json'], { |
| 49 | + stdio: 'inherit', |
| 50 | + preferLocal: true, |
| 51 | + }), |
| 52 | + ]); |
| 53 | + } else { |
| 54 | + await ctx.rebuild(); |
| 55 | + await ctx.dispose(); |
| 56 | + |
| 57 | + await buildMetadataSchema(); |
| 58 | + |
| 59 | + if (production) { |
| 60 | + // rename name in `package.json` to match extension name on store |
| 61 | + const pkgJSON = JSON.parse(fs.readFileSync('./package.json', { encoding: 'utf8' })); |
| 62 | + |
| 63 | + pkgJSON.name = 'tutorialkit'; |
| 64 | + |
| 65 | + fs.writeFileSync('./package.json', JSON.stringify(pkgJSON, undefined, 2), 'utf8'); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +async function buildMetadataSchema() { |
| 71 | + const schema = await new Promise((resolve) => { |
| 72 | + const worker = new Worker(join(__dirname, './load-schema-worker.mjs')); |
| 73 | + worker.on('message', (value) => resolve(value)); |
| 74 | + }); |
| 75 | + |
| 76 | + fs.mkdirSync('./dist', { recursive: true }); |
| 77 | + fs.writeFileSync('./dist/schema.json', JSON.stringify(schema, undefined, 2), 'utf-8'); |
| 78 | + |
| 79 | + console.log('Updated schema.json'); |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * @type {import('esbuild').Plugin} |
| 84 | + */ |
| 85 | +const esbuildUMD2ESMPlugin = { |
| 86 | + name: 'umd2esm', |
| 87 | + setup(build) { |
| 88 | + build.onResolve({ filter: /^(vscode-.*-languageservice|jsonc-parser)/ }, (args) => { |
| 89 | + const pathUmdMay = require.resolve(args.path, { paths: [args.resolveDir] }); |
| 90 | + const pathEsm = pathUmdMay.replace('/umd/', '/esm/').replace('\\umd\\', '\\esm\\'); |
| 91 | + |
| 92 | + return { path: pathEsm }; |
| 93 | + }); |
| 94 | + }, |
| 95 | +}; |
| 96 | + |
| 97 | +main().catch((error) => { |
| 98 | + console.error(error); |
| 99 | + process.exit(1); |
| 100 | +}); |
| 101 | + |
| 102 | +/** |
| 103 | + * Debounce the provided function. |
| 104 | + * |
| 105 | + * @param {Function} fn Function to debounce |
| 106 | + * @param {number} duration Duration of the debounce |
| 107 | + * @returns {Function} Debounced function |
| 108 | + */ |
| 109 | +function debounce(fn, duration) { |
| 110 | + let timeoutId = 0; |
| 111 | + |
| 112 | + return function () { |
| 113 | + clearTimeout(timeoutId); |
| 114 | + |
| 115 | + timeoutId = setTimeout(fn.bind(this), duration, ...arguments); |
| 116 | + }; |
| 117 | +} |
0 commit comments