Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@ardentcode/tsc

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ardentcode/tsc - npm Package Compare versions

Comparing version
0.1.0
to
0.1.1
+46
dist/cli.js
#!/usr/bin/env node
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const magic_1 = require("./magic");
const processExit_1 = require("./utils/processExit");
const argumentParser_1 = require("./utils/argumentParser");
const logger_1 = require("./logger/logger");
function main(argv) {
return __awaiter(this, void 0, void 0, function* () {
const argParser = new argumentParser_1.ArgumentParser(argv);
const magic = new magic_1.Magic(argParser);
logger_1.logger.setVerbose(argParser.hasFlag("verbose"));
if (argParser.hasFlag("purge")) {
yield magic.purgeCache();
return;
}
else if (argParser.hasFlag("help")) {
logger_1.logger.info(argParser.help());
return;
}
else {
logger_1.logger.debug("Compilation starts...");
try {
const { cacheFilesUsed, compiledFiles } = yield magic.compile();
logger_1.logger.info(`Done. ${cacheFilesUsed} files taken from cache, ${compiledFiles} files compiled.`);
process.exit(processExit_1.ProcessExit.Success);
}
catch (error) {
logger_1.logger.error("Unexpected error occurred:");
logger_1.logger.error(error);
process.exit(processExit_1.ProcessExit.FatalException);
}
}
});
}
main(process.argv.slice(2));
//# sourceMappingURL=cli.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.logger = exports.Logger = void 0;
class Logger {
constructor() {
this.printVerbose = false;
this.info = (info) => console.log(info);
this.error = (err) => console.error(err);
this.warn = (warning) => console.warn(warning);
this.debug = (info) => this.printVerbose && console.log(info);
}
setVerbose(printVerbose) {
this.printVerbose = printVerbose;
}
}
exports.Logger = Logger;
exports.logger = new Logger();
//# sourceMappingURL=logger.js.map
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Magic = void 0;
const getConfig_1 = __importDefault(require("./utils/getConfig"));
const path_1 = require("path");
const typescript_1 = __importDefault(require("typescript"));
const fs_1 = __importDefault(require("fs"));
const getOutputFilename_1 = require("./utils/getOutputFilename");
const createFile_1 = __importDefault(require("./utils/createFile"));
const buildCache_1 = require("../../build-cache/dist/buildCache");
const logger_1 = require("./logger/logger");
class Magic {
constructor(argsParser) {
this.argsParser = argsParser;
this.config = (0, getConfig_1.default)(argsParser.options);
this.tsMetadata = this.getTSMetadata();
this.cache = this.createCache();
this.program = this.getTSProgram();
}
compile() {
return __awaiter(this, void 0, void 0, function* () {
yield this.cache.validateCache(this.tsMetadata);
let cacheFilesUsed = 0;
let compiledFiles = 0;
const fileNames = this.config.fileNames;
for (const filePath of fileNames) {
const takenFromCache = yield this.compileFile(filePath);
logger_1.logger.debug(`${filePath} ${takenFromCache ? "taken from cache" : "compiled"}.`);
takenFromCache ? cacheFilesUsed++ : compiledFiles++;
}
yield this.cache.saveMetadata(this.tsMetadata);
return {
cacheFilesUsed,
compiledFiles,
};
});
}
purgeCache() {
return __awaiter(this, void 0, void 0, function* () {
yield this.cache.purge();
logger_1.logger.info(`Cache purged`);
});
}
createCache() {
const cacheSeed = this.getCacheSeed();
logger_1.logger.debug(`Cache mounted on ${process.cwd()} with seed: ${cacheSeed}`);
return new buildCache_1.BuildCache(cacheSeed, process.cwd(), this.argsParser.hasFlag("verbose"));
}
getTSProgram() {
const cliFileNames = this.argsParser.fileNames;
const { fileNames, compilerOptions } = this.config;
const program = typescript_1.default.createProgram(cliFileNames.length === 0 ? fileNames : cliFileNames, Object.assign(Object.assign({}, compilerOptions), { emitDeclarationOnly: true }));
return program;
}
getTSMetadata() {
const { options } = this.argsParser;
const tsConfigOptions = this.config.tsConfigOptions;
return {
cliOptions: options,
tsVersion: typescript_1.default.version,
compilerOptions: tsConfigOptions,
};
}
transpileModule(filePath) {
const code = this.getSourceCode(filePath);
const options = this.config.compilerOptions;
try {
return typescript_1.default.transpileModule(code, {
compilerOptions: options,
fileName: (0, path_1.basename)(filePath),
});
}
catch (e) {
logger_1.logger.error(`Compilation failed for ${filePath}`);
throw e;
}
}
compileFile(filePath) {
return __awaiter(this, void 0, void 0, function* () {
const sourceCodeHash = this.getSourceCodeHash(filePath);
const cacheEntry = yield this.getCacheEntry(filePath, sourceCodeHash);
if (cacheEntry) {
this.createFilesFromCacheEntry(cacheEntry.files);
return true;
}
const cacheEntryFiles = this.createFileCacheEntry(filePath);
this.createFilesFromCacheEntry(cacheEntryFiles);
yield this.cache.set(filePath, sourceCodeHash, cacheEntryFiles);
return false;
});
}
getCacheEntry(filePath, sourceCodeHash) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.cache.get(filePath, sourceCodeHash);
});
}
createFilesFromCacheEntry(cacheEntryFiles) {
cacheEntryFiles.forEach(({ filepath, content }) => {
(0, createFile_1.default)(filepath, content);
});
}
createFileCacheEntry(filePath) {
const options = this.config.compilerOptions;
const transpiled = this.transpileModule(filePath);
const newFilePath = (0, getOutputFilename_1.getOutputFilename)(options.outDir || "dist", filePath);
const cacheEntryFiles = [
{ filepath: newFilePath, content: transpiled.outputText },
];
if (options.declaration) {
this.program.emit(this.program.getSourceFile(filePath), (fileName, content) => cacheEntryFiles.push({
filepath: fileName.replace(".js", ".d.ts"),
content,
}));
}
if (transpiled.sourceMapText) {
cacheEntryFiles.push({
filepath: newFilePath + ".map",
content: transpiled.sourceMapText,
});
}
return cacheEntryFiles;
}
getCacheSeed() {
return JSON.stringify(this.tsMetadata.cliOptions);
}
getSourceCodeHash(filePath) {
const code = this.getSourceCode(filePath);
return this.cache.hashEntry(code);
}
getSourceCode(filePath) {
return fs_1.default.readFileSync(filePath, "utf8");
}
}
exports.Magic = Magic;
//# sourceMappingURL=magic.js.map
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArgumentParser = void 0;
const typescript_1 = __importDefault(require("typescript"));
const constants_1 = require("./constants");
class ArgumentParser {
constructor(argv) {
this.argv = argv;
const { fileNames, options } = typescript_1.default.parseCommandLine(argv);
this.fileNames = fileNames;
this.options = options;
}
hasFlag(flag, alias) {
return (this.argv.indexOf(`--${flag}`) >= 0 || this.argv.indexOf(`-${alias}`) >= 0);
}
help() {
return constants_1.HELP_MESSAGE;
}
}
exports.ArgumentParser = ArgumentParser;
//# sourceMappingURL=argumentParser.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HELP_MESSAGE = void 0;
exports.HELP_MESSAGE = `MAGIC.js - Usage
Commands:
-h, --help Prints avaliable commands
-p, --purge Deletes the entire contents of the cache folder
--verbose More verbose output`;
//# sourceMappingURL=constants.js.map
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const path_1 = require("path");
const logger_1 = require("../logger/logger");
const createFile = (path, data) => {
const dirName = (0, path_1.dirname)(path);
if (!fs_1.default.existsSync(dirName)) {
logger_1.logger.debug(`Directory ${dirName} created.`);
fs_1.default.mkdirSync(dirName, {
recursive: true,
});
}
fs_1.default.writeFileSync(path, data);
logger_1.logger.debug(`Created ${(0, path_1.basename)(path)}`);
};
exports.default = createFile;
//# sourceMappingURL=createFile.js.map
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const typescript_1 = __importDefault(require("typescript"));
const logger_1 = require("../logger/logger");
const getConfig = (options) => {
const configFileName = typescript_1.default.findConfigFile("./", typescript_1.default.sys.fileExists, options.project);
if (!configFileName) {
logger_1.logger.error("Could not find a valid 'tsconfig.json'.");
throw new Error("Could not find a valid 'tsconfig.json'.");
}
logger_1.logger.debug(`Config loaded from ${configFileName}`);
const configSourcesFile = typescript_1.default.readJsonConfigFile(configFileName, typescript_1.default.sys.readFile);
const { fileNames, options: tsConfigOptions } = typescript_1.default.parseJsonSourceFileConfigFileContent(configSourcesFile, typescript_1.default.sys, "./");
let compilerOptions = Object.assign(Object.assign({}, tsConfigOptions), options);
return {
fileNames: fileNames.filter((fileName) => !fileName.endsWith(".d.ts")),
compilerOptions,
tsConfigOptions: tsConfigOptions,
};
};
exports.default = getConfig;
//# sourceMappingURL=getConfig.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getOutputFilename = void 0;
function getOutputFilename(outDir, sourceFilename) {
return sourceFilename.replace("src", outDir).replace(".ts", ".js");
}
exports.getOutputFilename = getOutputFilename;
//# sourceMappingURL=getOutputFilename.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=metadata.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProcessExit = void 0;
var ProcessExit;
(function (ProcessExit) {
ProcessExit[ProcessExit["Success"] = 0] = "Success";
ProcessExit[ProcessExit["FatalException"] = 1] = "FatalException";
})(ProcessExit = exports.ProcessExit || (exports.ProcessExit = {}));
//# sourceMappingURL=processExit.js.map
+1
-1
{
"name": "@ardentcode/tsc",
"version": "0.1.0",
"version": "0.1.1",
"description": "",

@@ -5,0 +5,0 @@ "main": "index.js",