Comparing version 1.1.0 to 1.1.1-dev.20210905.1
module.exports = { | ||
dependencies: [ 'glob' ], | ||
config: [ 'compilerOptions.allowTs' ], | ||
arguments: { | ||
copyFiles: [ 'cf', 'copy-files' ], | ||
excludeFiles: [ 'ef', 'exclude-files' ] | ||
onInitCompilation: function(api) { | ||
// This will ensure the unsupported allowTs attribute is ignored | ||
api.tsconfig.ignore('compilerOptions.allowTs'); | ||
}, | ||
onPostCompilation: function() { | ||
onPostCompilation: function(api) { | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const glob = require('glob'); | ||
const include = this.tsconfig.include?.map(file => path.resolve(this.tsconfigDir, file)); | ||
if (this.args.copyFiles) { | ||
include.push(...this.args.copyFiles?.split(' ')?.map(file => path.resolve(process.cwd(), file.trim()))); | ||
// Add included files from config | ||
const includedFiles = new Set(); | ||
for (const includedFromConfig of api.tsconfig.include) { | ||
const files = glob.sync(includedFromConfig, { cwd: api.tsconfig.directory }); | ||
for (const file of files) { | ||
includedFiles.add(file); | ||
} | ||
} | ||
const ignore = this.tsconfig.exclude?.map(file => path.resolve(this.tsconfigDir, file)); | ||
if (this.args.excludeFiles) { | ||
ignore.push(...this.args.excludeFiles?.split(' ')?.map(file => path.resolve(process.cwd(), file.trim()))); | ||
// Remove excluded files from config | ||
for (const excludedFromConfig of api.tsconfig.exclude) { | ||
const files = glob.sync(excludedFromConfig, { cwd: api.tsconfig.directory }); | ||
for (const file of files) { | ||
includedFiles.delete(file); | ||
} | ||
} | ||
ignore.push( | ||
path.resolve(this.tsconfigDir, this.tsconfig.compilerOptions.outDir), | ||
path.resolve(this.tsconfigDir, `${this.tsconfig.compilerOptions.outDir}/**/*`) | ||
); | ||
const files = include?.reduce((files, file) => [ ...files, ...this.glob.sync(file, { ignore })], []) || []; | ||
for (const file of files) { | ||
if (file.endsWith('.js') || (!this.tsconfig.compilerOptions?.allowTs && file.endsWith('.ts'))) continue; | ||
const outFile = file.replace(this.tsconfigDir, path.resolve(this.tsconfigDir, this.tsconfig.compilerOptions.outDir)); | ||
if (!fs.existsSync(path.dirname(outFile))) { | ||
fs.mkdirSync(path.dirname(outFile), { recursive: true }); | ||
} | ||
if (fs.lstatSync(file).isDirectory() && !fs.existsSync(outFile)) { | ||
fs.mkdirSync(outFile, { recursive: true }); | ||
} | ||
if (fs.lstatSync(file).isFile()) { | ||
const fileContent = fs.readFileSync(file); | ||
fs.writeFileSync(outFile, fileContent); | ||
} | ||
} | ||
console.log(Array.from(includedFiles)); | ||
} | ||
} |
module.exports = { | ||
dependencies: [ 'chalk' ], // Needed NPM Modules, these modules will be installed using `npm i <dependency>...` | ||
config: [ 'compilerOptions.message' ], // Custom properties introduced, ex: "compilerOptions.someProperty" in tsconfig | ||
arguments: { // Custom arguments when using tsc | ||
messageVarName: [ 'msg', 'message' ] // Used like "tsc --message 'Cool Message'" or "tsc -msg 'Cool Alias'" | ||
}, | ||
onInitCompilation: function() { | ||
console.log(this.chalk.cyan('onInitCompilation is executed before tsc has started compilation')); | ||
onInitCompilation: function(api) { | ||
const chalk = require('chalk'); | ||
console.log('Below is the `this` context in the onInitCompilation function:'); | ||
console.log(this); | ||
console.log(chalk.cyan('onInitCompilation is executed before tsc has started compilation')); | ||
console.log("Message:", this.messageVarName); | ||
console.log('Below is the api context in the onInitCompilation function:'); | ||
console.log(api); | ||
console.log("Init Message:", api.tsconfig.message); | ||
api.tsconfig.message = "POST MESSAGE"; | ||
}, | ||
onPostCompilation: function() { | ||
console.log(this.chalk.magenta('onPostCompilation is executed after tsc has completed compilation')); | ||
onPostCompilation: function(api) { | ||
const chalk = require('chalk'); | ||
console.log('Below is the `this` context in the onPostCompilation function:'); | ||
console.log(this); | ||
console.log(chalk.magenta('onPostCompilation is executed after tsc has completed compilation')); | ||
console.log('Below is the api context in the onPostCompilation function:'); | ||
console.log(api); | ||
console.log("Post Message:", api.tsconfig.message); | ||
} | ||
} |
@@ -7,2 +7,3 @@ const fs = require('fs'); | ||
const installDependencies = require('./utils/installDependencies'); | ||
const createTSConfigAPI = require('./utils/createTSConfigAPI'); | ||
@@ -15,22 +16,11 @@ const tsconfigDir = process.cwd(); | ||
const tsconfig = JSON5.parse(tsconfigStr); | ||
const hookModules = [], dependencies = [], config = [], arguments = {}; | ||
const hookModules = [], dependencies = []; | ||
for (const hook of tsconfig.hooks || []) { | ||
// Get Hook by URL, by Official ID, or by Path | ||
const hookModule = requireHook(hook, tsconfigDir); | ||
// Add dependencies | ||
dependencies.push(...hookModule.dependencies); | ||
// Add custom config properties | ||
config.push(...hookModule.config); | ||
// Handle arguments | ||
for (const argVariable in hookModule.arguments) { | ||
const resolvedArgs = hookModule.arguments[argVariable].map(arg => arg.length > 3 ? `--${arg}` : `-${arg}`); | ||
const argIndex = process.argv.findIndex(arg => resolvedArgs.includes(arg)); | ||
if (argIndex >= 0) { | ||
arguments[argVariable] = process.argv[argIndex+1]; | ||
process.argv.splice(argVariable, 2); | ||
} | ||
} | ||
// Add hookModule | ||
@@ -43,26 +33,14 @@ hookModules.push(hookModule); | ||
if (config.length) { | ||
const copyTSConfig = JSON5.parse(tsconfigStr); | ||
for (const prop of config) { | ||
eval(`delete copyTSConfig?.${prop.replace(/\./g, '?.')}`); | ||
} | ||
fs.writeFileSync(tsconfigPath, JSON.stringify(copyTSConfig)); | ||
process.on('exit', () => fs.writeFileSync(tsconfigPath, tsconfigStr)); | ||
} | ||
const hookBinding = { | ||
...dependencies.reduce((deps, dep) => { | ||
deps[dep] = require(dep); | ||
return deps; | ||
}, {}), | ||
tsconfig, | ||
tsconfigDir, | ||
tsconfigPath, | ||
args: arguments | ||
const api = { | ||
tsconfig: createTSConfigAPI(tsconfig, tsconfigPath) | ||
}; | ||
// Call each hook | ||
for (const hookModule of hookModules || []) { | ||
hookModule?.onInitCompilation?.bind(hookBinding)(); | ||
process.on('exit', hookModule?.onPostCompilation?.bind(hookBinding)); | ||
hookModule?.onInitCompilation?.(api); | ||
process.on('exit', () => hookModule?.onPostCompilation?.(api)); | ||
} | ||
// Hooks may mutate the config, so write the original config back | ||
process.on('exit', () => fs.writeFileSync(tsconfigPath, tsconfigStr)); | ||
} |
{ | ||
"name": "tsc-hooks", | ||
"version": "1.1.0", | ||
"version": "1.1.1-dev.20210905.1", | ||
"description": "Add tsc compiler hooks to your TypeScript project", | ||
@@ -27,5 +27,8 @@ "main": "run.js", | ||
"json5": "^2.2.0", | ||
"typescript": "^4.3.2", | ||
"yargs-parser": "^20.2.9" | ||
"typescript": "^4.3.2" | ||
}, | ||
"devDependencies": { | ||
"chalk": "^4.1.2", | ||
"glob": "^7.1.7" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
2
15
163
9
108020
2
2
- Removedyargs-parser@^20.2.9
- Removedyargs-parser@20.2.9(transitive)