@adonisjs/ace
Version:
Commandline apps framework used by AdonisJs
62 lines (61 loc) • 1.91 kB
JavaScript
;
/*
* @adonisjs/ace
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Generator = void 0;
const fs_extra_1 = require("fs-extra");
const File_1 = require("./File");
/**
* Exposes the API to generate entity files, like project
* `Controllers`, `Models` and so on.
*/
class Generator {
constructor(command, destinationDir) {
this.command = command;
this.destinationDir = destinationDir;
this.files = [];
}
/**
* Add a new file to the files generator. You can add multiple files
* together and they will be created when `run` is invoked.
*/
addFile(name, options) {
const file = new File_1.GeneratorFile(name, options);
if (this.destinationDir) {
file.destinationDir(this.destinationDir);
}
this.files.push(file);
return file;
}
/**
* Run the generator and create all files registered using `addFiles`
*/
async run() {
for (let file of this.files) {
const fileJSON = file.toJSON();
const exists = await (0, fs_extra_1.pathExists)(fileJSON.filepath);
if (exists) {
this.command.logger.action('create').skipped(fileJSON.relativepath, 'File already exists');
}
else {
await (0, fs_extra_1.outputFile)(fileJSON.filepath, fileJSON.contents);
file.state = 'persisted';
this.command.logger.action('create').succeeded(fileJSON.relativepath);
}
}
return this.files;
}
/**
* Clear the registered files from the generator
*/
clear() {
this.files = [];
}
}
exports.Generator = Generator;