Comparing version 1.0.8 to 1.0.9-dev.20210708.1
@@ -1,4 +0,2 @@ | ||
- Pass tsc flag options as arguments to hooks | ||
- Only cache hooks that are requested, then take from GitHub/put hooks in npmignore | ||
- Add third party hook capability | ||
- Create Contribution Docs and make a create-hook script | ||
@@ -8,1 +6,2 @@ - Add lintr | ||
- Add GitHub actions | ||
- Allow asynchronous calls |
@@ -1,17 +0,18 @@ | ||
const glob = require('glob'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
module.exports = { | ||
dependencies: [ 'glob' ], | ||
customProperties: [ 'compilerOptions.allowTS' ], | ||
onPostCompilation: function() { | ||
const glob = require('glob'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
module.exports = (tsconfig, tsconfigPath) => { | ||
process.on('exit', () => { | ||
const tsconfigDir = path.dirname(tsconfigPath); | ||
const include = tsconfig.include?.map(file => path.resolve(tsconfigDir, file)); | ||
const ignore = tsconfig.exclude?.map(file => path.resolve(tsconfigDir, file)); | ||
const include = this.tsconfig.include?.map(file => path.resolve(this.tsconfigDir, file)); | ||
const ignore = this.tsconfig.exclude?.map(file => path.resolve(this.tsconfigDir, file)); | ||
const files = include?.reduce((files, file) => glob.sync(file, { ignore }), []) || []; | ||
for (const file of files) { | ||
if (file.endsWith('.js') || (!tsconfig.compilerOptions?.allowTS && file.endsWith('.ts'))) continue; | ||
if (file.endsWith('.js') || (!this.tsconfig.compilerOptions?.allowTS && file.endsWith('.ts'))) continue; | ||
const relative = file.replace(path.resolve(file, path.relative(file, tsconfigDir)), '').split('/').splice(2).join('/'); | ||
const target = path.resolve(tsconfigDir, tsconfig.compilerOptions.outDir, relative); | ||
const target = path.resolve(this.tsconfigDir, this.tsconfig.compilerOptions.outDir, relative); | ||
@@ -31,3 +32,3 @@ if (!fs.existsSync(path.dirname(target))) { | ||
} | ||
}); | ||
}; | ||
} | ||
} |
const fs = require('fs'); | ||
const crypto = require('crypto'); | ||
const { execSync } = require('child_process'); | ||
const JSON5 = require('json5'); | ||
const path = require('path'); | ||
const TS_CONFIG_PATH = path.resolve(process.cwd(), 'tsconfig.json'); | ||
const tsconfigDir = process.cwd(); | ||
const tsconfigPath = path.resolve(process.cwd(), 'tsconfig.json'); | ||
if (fs.existsSync(TS_CONFIG_PATH)) { | ||
const tsconfig = JSON5.parse(fs.readFileSync(TS_CONFIG_PATH)); | ||
function requireHook(hook) { | ||
if (hook.endsWith('.js')) { | ||
try { | ||
new URL(hook); | ||
const hashPath = path.resolve(__dirname, '../hooks', `${crypto.createHash('md5').update(hook).digest('hex')}.js`); | ||
if (!fs.existsSync(hashPath)) { | ||
execSync(`curl ${hook} -o ${hashPath} -s`); | ||
} | ||
return require(hashPath); | ||
} catch (error) { | ||
return require(path.resolve(tsconfigDir, hook)); | ||
} | ||
} else { | ||
return require(path.resolve(__dirname, '../hooks', hook)); | ||
} | ||
} | ||
if (fs.existsSync(tsconfigPath)) { | ||
const tsconfigStr = fs.readFileSync(tsconfigPath); | ||
const tsconfig = JSON5.parse(tsconfigStr); | ||
const hookModules = [], dependencies = [], config = [], arguments = {}; | ||
for (const hook of tsconfig.hooks || []) { | ||
try { | ||
require(path.resolve(__dirname, '../hooks', `${hook}.js`))(tsconfig, TS_CONFIG_PATH); | ||
} catch (error) { | ||
if (error.code === 'MODULE_NOT_FOUND') { | ||
console.error([ | ||
`Hook named "${hook}" does not exist in tsc-hooks repository. If you are looking to create your own hook or`, | ||
'want to see what hooks are available visit https://github.com/swimauger/tsc-hooks' | ||
].join('\n')); | ||
} else { | ||
console.error(`Hook Module named "${hook}" threw an error, more information bellow:`); | ||
console.error(error); | ||
const hookModule = requireHook(hook); | ||
// 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 | ||
hookModules.push(hookModule); | ||
} | ||
if (dependencies.length) { | ||
execSync(`npm i ${Array.from(new Set(dependencies)).join(' ')}`, { cwd: path.resolve(__dirname, '..') }); | ||
} | ||
if (config.length) { | ||
const copyTSConfig = JSON5.parse(tsconfigStr); | ||
for (const prop of config) { | ||
eval(`delete copyTSConfig.${prop}`); | ||
} | ||
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; | ||
}, {}), | ||
config: tsconfig, | ||
tsconfigDir, | ||
tsconfigPath, | ||
args: arguments | ||
}; | ||
for (const hookModule of hookModules || []) { | ||
hookModule?.onInitCompilation?.bind(hookBinding)(); | ||
process.on('exit', hookModule?.onPostCompilation?.bind(hookBinding)); | ||
} | ||
} |
{ | ||
"name": "tsc-hooks", | ||
"version": "1.0.8", | ||
"version": "1.0.9-dev.20210708.1", | ||
"description": "Add tsc compiler hooks to your TypeScript project", | ||
@@ -20,3 +20,2 @@ "main": "run.js", | ||
"dependencies": { | ||
"glob": "^7.1.7", | ||
"json5": "^2.2.0", | ||
@@ -23,0 +22,0 @@ "typescript": "^4.3.2" |
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
106841
2
12
132
2
10
2
- Removedglob@^7.1.7
- Removedbalanced-match@1.0.2(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removedfs.realpath@1.0.0(transitive)
- Removedglob@7.2.3(transitive)
- Removedinflight@1.0.6(transitive)
- Removedinherits@2.0.4(transitive)
- Removedminimatch@3.1.2(transitive)
- Removedonce@1.4.0(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedwrappy@1.0.2(transitive)