118 lines
3.1 KiB
JavaScript
118 lines
3.1 KiB
JavaScript
import * as esbuild from 'esbuild';
|
||
import fs from 'fs';
|
||
import path from 'path';
|
||
import { fileURLToPath } from 'url';
|
||
import { rewriteGeneratedVarKeywords } from './rewriteGeneratedVars.mjs';
|
||
|
||
const root = path.join(path.dirname(fileURLToPath(import.meta.url)), '..');
|
||
|
||
const department = process.argv[2];
|
||
if (!department) {
|
||
console.error('Usage: npm run build -- <department>');
|
||
process.exit(1);
|
||
}
|
||
|
||
const departmentsPath = path.join(root, 'departments.json');
|
||
const departments = JSON.parse(fs.readFileSync(departmentsPath, 'utf8'));
|
||
const config = departments[department];
|
||
|
||
if (!config || !config.scriptId) {
|
||
console.error(`Unknown department or missing scriptId: ${department}`);
|
||
process.exit(1);
|
||
}
|
||
|
||
const entry = path.join(root, 'src', 'departments', department, 'entry.ts');
|
||
if (!fs.existsSync(entry)) {
|
||
console.error(`No entry file: ${entry}`);
|
||
process.exit(1);
|
||
}
|
||
|
||
const outDir = path.join(root, 'dist', department);
|
||
fs.mkdirSync(outDir, { recursive: true });
|
||
|
||
const outfile = path.join(outDir, 'Code.js');
|
||
|
||
await esbuild.build({
|
||
entryPoints: [entry],
|
||
bundle: true,
|
||
outfile,
|
||
format: 'iife',
|
||
globalName: '__gas',
|
||
target: 'es2020',
|
||
logLevel: 'info',
|
||
});
|
||
|
||
const bundled = fs.readFileSync(outfile, 'utf8');
|
||
const withoutVar = rewriteGeneratedVarKeywords(bundled);
|
||
if (/\bvar\b/.test(withoutVar)) {
|
||
throw new Error('В бандле осталось ключевое слово var');
|
||
}
|
||
fs.writeFileSync(outfile, withoutVar);
|
||
|
||
appendGasEntryWrappers(outfile);
|
||
|
||
fs.copyFileSync(
|
||
path.join(root, 'appsscript.json'),
|
||
path.join(outDir, 'appsscript.json')
|
||
);
|
||
|
||
const htmlFiles = config.htmlFiles || [];
|
||
for (const fileName of htmlFiles) {
|
||
const source = path.join(root, 'src', 'html', fileName);
|
||
if (!fs.existsSync(source)) {
|
||
console.error(`Missing HTML file: ${source}`);
|
||
process.exit(1);
|
||
}
|
||
fs.copyFileSync(source, path.join(outDir, fileName));
|
||
}
|
||
|
||
fs.writeFileSync(
|
||
path.join(outDir, '.clasp.json'),
|
||
JSON.stringify(
|
||
{
|
||
scriptId: config.scriptId,
|
||
rootDir: '.',
|
||
scriptExtensions: ['.js', '.gs'],
|
||
htmlExtensions: ['.html'],
|
||
jsonExtensions: ['.json'],
|
||
skipSubdirectories: true,
|
||
},
|
||
null,
|
||
2
|
||
)
|
||
);
|
||
|
||
console.log(`Built ${department} → dist/${department}`);
|
||
|
||
function collectGasFunctionNames(code) {
|
||
const names = new Set(['onOpen', 'menuSetup']);
|
||
|
||
for (const match of code.matchAll(/handlers:\s*\{([^}]+)\}/g)) {
|
||
for (const part of match[1].split(',')) {
|
||
const name = part.trim().split(/[:\s]/)[0];
|
||
if (/^[A-Za-z_$][\w$]*$/.test(name)) {
|
||
names.add(name);
|
||
}
|
||
}
|
||
}
|
||
|
||
return [...names].sort();
|
||
}
|
||
|
||
function appendGasEntryWrappers(filePath) {
|
||
const code = fs.readFileSync(filePath, 'utf8');
|
||
const names = collectGasFunctionNames(code);
|
||
|
||
if (!names.includes('onOpen')) {
|
||
throw new Error('Сборка не нашла onOpen для глобальных обёрток GAS');
|
||
}
|
||
|
||
const wrappers = names
|
||
.map((name) =>
|
||
`function ${name}() { return (__gas.default || __gas).${name}.apply(this, arguments); }`
|
||
)
|
||
.join('\n');
|
||
|
||
fs.writeFileSync(filePath, `${code}\n${wrappers}\n`);
|
||
}
|