ask-cli-x
Version:
Alexa Skills Kit (ASK) Command Line Interfaces
79 lines (78 loc) • 3.66 kB
JavaScript
;
const fs = require("fs");
const path = require("path");
const acdl = require("@alexa/acdl");
const { COMPILER } = require("../../utils/constants");
const Messenger = require("../../view/messenger");
const CliError = require("../../exceptions/cli-error");
function cleanOutputDir(directoryPath) {
if (fs.existsSync(directoryPath)) {
fs.rmdirSync(directoryPath, { recursive: true });
}
}
module.exports = class Compiler {
constructor(configuration) {
const { profile } = configuration;
this.profile = profile;
}
async compile(compilerInput) {
const projectConfig = await acdl.loadProjectConfig(compilerInput.rootDir, this.profile);
const outDirPath = path.join(projectConfig.rootDir, projectConfig.outDir);
const skillPackageOutputDir = path.join(outDirPath, COMPILER.TARGETDIR);
// Remove the old generated contents
cleanOutputDir(skillPackageOutputDir);
const project = await acdl.loadProject(projectConfig);
// isSkill is true for skills typically with a skill-package directory.
// A module is typically a reusable component
if (project.isSkill()) {
await this.compileSkill(compilerInput, project);
}
else {
await this.compileModule(compilerInput, project);
}
}
/**
* Compiles skill acdl into ASKIR json using bundleProject
*/
async compileSkill(compilerInput, project) {
Messenger.getInstance().info("************ Compiling Skill ************");
Messenger.getInstance().info(`Skill package directory: ${project.config.sourceRootDir}`);
if (!compilerInput.skipValidation) {
const errors = acdl.validateProject(project, true);
if (errors.length > 0) {
acdl.logProjectErrors(errors);
if (errors.filter((err) => err.code.category !== "Warning" /* acdl.ErrorCategory.Warning */).length > 0) {
throw new CliError(`${COMPILER.ASK_COMPILER} Compilation Failed.`);
}
}
}
const bundleOutput = await acdl.bundleProject(project, {
outDir: compilerInput.outDir,
});
if (!compilerInput.skipValidation && bundleOutput.errors.length > 0) {
acdl.logProjectErrors(bundleOutput.errors);
throw new CliError(`${COMPILER.ASK_COMPILER} Compilation Failed.`);
}
Messenger.getInstance().info("Compiled Skill Artifacts successfully," +
` the output is saved at ${compilerInput.outDir || path.join(process.cwd(), project.config.outDir)}.`);
}
/**
* Compiles a module, typically a reusable component, using synthesizeProject
*/
async compileModule(compilerInput, project) {
Messenger.getInstance().info("************ Compiling Module ************");
Messenger.getInstance().info(`Source directory: ${project.config.srcDir}`);
if (!compilerInput.skipValidation) {
const errors = acdl.validateProject(project, false);
if (errors.length > 0) {
acdl.logProjectErrors(errors);
if (errors.filter((err) => err.code.category !== "Warning" /* acdl.ErrorCategory.Warning */).length > 0) {
throw new CliError(`${COMPILER.ASK_COMPILER} Compilation Failed.`);
}
}
}
await acdl.synthesizeProject(project);
Messenger.getInstance().info("Compiled Module Artifacts successfully," +
` the output is saved at ${compilerInput.outDir || path.join(process.cwd(), project.config.outDir)}.`);
}
};