Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tsc-hooks

Package Overview
Dependencies
Maintainers
1
Versions
48
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tsc-hooks - npm Package Compare versions

Comparing version 1.0.8 to 1.0.9-dev.20210708.1

hooks/example.js

3

docs/TODO.md

@@ -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"

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc