kt-extendscript-builder
Version:
Vite based builder for transpile TypeScript to ExtendScript
103 lines (102 loc) • 3.84 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Builder = void 0;
const options_1 = require("../options");
const createViteConfig_1 = require("../config/createViteConfig");
const createRollupConfig_1 = require("../config/createRollupConfig");
const vite_1 = require("vite");
const Cleaner_1 = require("./Cleaner");
const OptionsParser_1 = require("../options/OptionsParser");
/**
* Main class of the build system.
* Coordinates the build process with a simple and clear structure.
*/
class Builder {
constructor() {
this.options = {};
this.viteConfig = {};
this.optionsResolver = new options_1.OptionsResolver();
}
/**
* Executes the build process with the configured options
* This method executes the complete clean-build-clean process
*/
async build(command) {
// If no options have been configured, we do it automatically
if (Object.keys(this.options).length === 0) {
this.options = this.optionsResolver.resolve(command);
}
try {
// 1. Initial cleanup if needed
await this.cleanIfNeeded('before');
// 2. Build only if it's not an exclusive cleaning command
const isCleanOnly = process.argv.includes('clean-only');
if (!isCleanOnly) {
// Configure Vite and Rollup
const viteConfig = (0, createViteConfig_1.createViteConfig)(this.options);
viteConfig.extendScriptConfig = (0, createRollupConfig_1.createRollupConfig)(this.options);
this.viteConfig = (0, vite_1.defineConfig)(viteConfig);
// Run build or watch according to configuration
if (this.options.watch) {
await this.viteConfig.extendScriptConfig.watch();
}
else {
await this.viteConfig.extendScriptConfig.build();
}
}
// 3. Final cleanup if needed
await this.cleanIfNeeded('after');
}
catch (error) {
console.error('Error during build process:', error);
throw error;
}
}
/**
* Convenience method that combines configuration and build
* To maintain compatibility with existing code
*/
async run() {
const commands = OptionsParser_1.OptionsParser.extractCommands();
if (commands.length === 0) {
await this.build();
}
for (const command of commands) {
await this.build(command);
this.resetOptions();
}
}
/**
* Determines if the directory should be cleaned based on the options
*/
shouldClean(stage) {
if (!this.options.clean)
return false;
// If it's a string, convert to array for uniform processing
const cleanOption = typeof this.options.clean === 'string'
? [this.options.clean]
: Array.isArray(this.options.clean)
? this.options.clean
: [];
// If 'false' was specified, don't clean anything
if (cleanOption.includes('false'))
return false;
// Clean if 'both' or the specific stage was specified
return cleanOption.includes('both') || cleanOption.includes(stage);
}
/**
* Cleans the output directory if necessary according to the configuration
*/
async cleanIfNeeded(stage) {
if (this.shouldClean(stage)) {
console.log(`Starting cleanup (${stage})...`);
await Cleaner_1.Cleaner.cleanDist(this.options);
}
}
resetOptions() {
this.options = {};
this.viteConfig = {};
this.optionsResolver = new options_1.OptionsResolver();
}
}
exports.Builder = Builder;