makit
Version:
Make in JavaScript done right!
120 lines (119 loc) • 4.15 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Context = void 0;
const path_1 = require("path");
const logger_1 = require("./utils/logger");
class Context {
constructor({ target, match, root, fs, make }) {
this.dependencies = [];
this.dynamicDependencies = [];
this.logger = logger_1.Logger.getOrCreate();
this.root = root;
this.match = match;
this.target = target;
this.fs = fs;
this.makeImpl = make;
}
async make(target) {
this.logger.debug('RUDE', 'context.make called with', logger_1.hlTarget(target), 'while making', logger_1.hlTarget(this.target));
this.dynamicDependencies.push(target);
return this.makeImpl(target);
}
async outputFile(filepath, content) {
filepath = this.toFullPath(filepath);
return this.writeFile(filepath, content).catch(async (e) => {
if (e.code === 'ENOENT') {
await this.mkdir(path_1.dirname(filepath), { recursive: true });
return this.writeFile(filepath, content);
}
throw e;
});
}
outputFileSync(filepath, content) {
filepath = this.toFullPath(filepath);
try {
return this.fs.writeFileSync(filepath, content);
}
catch (e) {
if (e.code === 'ENOENT') {
this.fs.mkdirSync(path_1.dirname(filepath), { recursive: true });
return this.writeFileSync(filepath, content);
}
throw e;
}
}
async readDependency(i = 0) {
if (i >= this.dependencies.length)
throw new Error(`cannot get ${i}th dependency,dependencieshis.deps.length} dependencies in total`);
return this.readFile(this.dependencyFullPath(i));
}
readDependencySync(i = 0) {
if (i >= this.dependencies.length)
throw new Error(`cannot get ${i}th dependency,dependencieshis.deps.length} dependencies in total`);
return this.readFileSync(this.dependencyFullPath(i), 'utf8');
}
targetFullPath() {
return this.toFullPath(this.target);
}
targetPath() {
return this.target;
}
dependencyFullPath(i = 0) {
return this.toFullPath(this.dependencies[i]);
}
dependencyPath(i = 0) {
return this.dependencies[i];
}
writeTarget(content) {
return this.outputFile(this.targetFullPath(), content);
}
writeTargetSync(content) {
return this.outputFileSync(this.targetFullPath(), content);
}
toFullPath(filename) {
return path_1.resolve(this.root, filename);
}
/**
* FileSystem Implements
*/
async mkdir(filepath, options) {
return this.fs.mkdir(this.toFullPath(filepath), options);
}
mkdirSync(filepath, options) {
return this.fs.mkdirSync(this.toFullPath(filepath), options);
}
async writeFile(filepath, content) {
return this.fs.writeFile(this.toFullPath(filepath), content);
}
writeFileSync(filepath, content) {
return this.fs.writeFileSync(this.toFullPath(filepath), content);
}
async readFile(filepath, encoding = 'utf8') {
return this.fs.readFile(this.toFullPath(filepath), encoding);
}
readFileSync(filepath, encoding = 'utf8') {
return this.fs.readFileSync(this.toFullPath(filepath), encoding);
}
unlinkSync(filepath) {
return this.fs.unlinkSync(this.toFullPath(filepath));
}
unlink(filepath) {
return this.fs.unlink(this.toFullPath(filepath));
}
existsSync(filepath) {
return this.fs.existsSync(this.toFullPath(filepath));
}
utimes(filepath, atime, utime) {
return this.fs.utimes(this.toFullPath(filepath), atime, utime);
}
utimesSync(filepath, atime, utime) {
return this.fs.utimesSync(this.toFullPath(filepath), atime, utime);
}
stat(filepath) {
return this.fs.stat(this.toFullPath(filepath));
}
statSync(filepath) {
return this.fs.statSync(this.toFullPath(filepath));
}
}
exports.Context = Context;