scraipt
Version:
Scrape away inefficient code during compile-time using AI
76 lines (75 loc) • 2.57 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.writeScraiptFile = exports.createScraiptFolder = exports.insertBeforeExtension = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
/**
Insert text before the last extension extension in the given path.
* @param path The path to insert the text.
* @param text The text to insert.
* @returns The new path with the text inserted.
*/
const insertBeforeExtension = (path, text) => {
const parts = path.split('.');
const extension = parts.pop();
if (!extension) {
return;
}
const newPath = parts.join('.') + text + '.' + extension;
return newPath;
};
exports.insertBeforeExtension = insertBeforeExtension;
/**
Get the scraipt build directory.
* @param buildPath The path of the build directory.
* @returns The scraipt build directory.
*/
const getScraiptDir = (buildPath) => {
if (!buildPath) {
buildPath = 'dist';
// If the default build path does not exist, and none is in the options, throw an error
if (!fs_1.default.existsSync(buildPath)) {
fs_1.default.mkdirSync(buildPath);
}
}
return path_1.default.join(buildPath, 'scraipt');
};
/**
Create a folder for outputting the transformed source code.
* @param buildPath The path of the build directory.
*/
const createScraiptFolder = (buildPath) => {
const scraiptDir = getScraiptDir(buildPath);
if (fs_1.default.existsSync(scraiptDir)) {
return;
}
fs_1.default.mkdirSync(scraiptDir);
};
exports.createScraiptFolder = createScraiptFolder;
/**
* Writes source data to a debug scraipt file
* @param filePath The path of the file to write
* @param data The data to write to the file
* @returns
*/
const writeScraiptFile = (filePath, data, buildPath) => {
const fileName = filePath.split('/').pop();
if (!fileName) {
return;
}
const scraiptDir = getScraiptDir(buildPath);
const sourcePath = path_1.default.join(scraiptDir, fileName);
if (!sourcePath) {
return;
}
// Write the transformed source code to a .scraipt file
const scraiptPath = (0, exports.insertBeforeExtension)(sourcePath, '.scraipt');
if (!scraiptPath) {
return;
}
fs_1.default.writeFileSync(scraiptPath, data);
};
exports.writeScraiptFile = writeScraiptFile;