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.1.2-dev.20211011.2 to 1.1.2-dev.20211113.1

docs/assets/defcon-levels.jpeg

54

docs/TODO.md

@@ -1,9 +0,47 @@

- Only cache hooks that are requested, then take from GitHub/put hooks in npmignore
# TODO / Agile Board
<details>
<summary>LEGEND</summary>
**Prioritization:**
<img src="./assets/defcon-levels.jpeg" align="right">
DEFCON 1 = Critical
DEFCON 2 = High
DEFCON 3 = Medium
DEFCON 4 = Low
DEFCON 5 = Very Low
**Difficulty:**
SP = Story Points (1-10)
</details>
## DEFCON 1
- <s>Create Contribution Docs and make a create-hook script</s>
- Add lintr
- Add GitHub actions
- Allow asynchronous calls
- Find a way to tell editors to ignore custom properties in tsconfig
- Add Babel to target lower version
- Add arguments
- Make tests run locally instead of needing a dev publish
## DEFCON 2
- Add tsc watch to the API (8 SP)
- Instead of writing over the tsconfig, create temp tsconfig and direct the tsc compiler to the temp config by modifying process args (7 SP)
- Update documentation to show that developers have access to creating local hooks and using hooks from urls (1 SP)
## DEFCON 3
- Add documentation place for each hook and update create hook script (2 SP)
- Add Babel to target lower version (4 SP)
- Make tests run locally instead of needing a dev publish (7 SP)
- Add arguments (4 SP)
## DEFCON 4
- Only cache hooks that are requested, then take from GitHub/put hooks in npmignore (6 SP)
- Add lintr (3 SP)
- Add GitHub actions (2 SP)
- Allow asynchronous calls (2 SP)
## DEFCON 5
- Find a way to tell editors to ignore custom properties in tsconfig (9 SP)

64

hooks/copy-files/copy-files.hook.js

@@ -1,13 +0,30 @@

let include, exclude;
const fs = require('fs');
const path = require('path');
const rootRegex = /^([^\/|\\])+/;
module.exports = {
dependencies: [ 'glob' ],
onInitCompilation: function(api) {
onInitCompilation(api) {
const glob = require('glob');
const cwd = api.tsconfig.compilerOptions?.rootDir || api.tsconfig.directory;
// This will ensure the unsupported allowTs attribute is ignored
api.tsconfig.ignore('compilerOptions.allowTs');
// Save include and exclude files
include = api.tsconfig.include ? [ ...api.tsconfig.include ] : [];
exclude = api.tsconfig.exclude ? [ ...api.tsconfig.exclude ] : [];
// Add included files from config
const includedFiles = new Set();
for (const includedFromConfig of api.tsconfig.include || []) {
const files = glob.sync(includedFromConfig, { cwd });
files.forEach(includedFiles.add.bind(includedFiles));
}
// Remove excluded files from config
for (const excludedFromConfig of api.tsconfig.exclude || []) {
const files = glob.sync(excludedFromConfig, { cwd });
files.forEach(includedFiles.delete.bind(includedFiles));
}
// Register files to watch on change
api.watch.apply(api, includedFiles);
// Remove include and exclude files that aren't typescript from tsconfig for compilation

@@ -23,23 +40,26 @@ api.tsconfig.include = api.tsconfig.include?.filter(file => file.endsWith('ts') || file.endsWith('*'));

},
onPostCompilation: function(api) {
const path = require('path');
const fs = require('fs');
const glob = require('glob');
const cwd = api.tsconfig?.compilerOptions?.rootDir || api.tsconfig.directory;
const rootRegex = /^([^\/|\\])+/;
onWatchCompilation(event, file, api) {
if (file.endsWith('js')) return;
if (file.endsWith('ts') && !api.tsconfig?.compilerOptions?.allowTs) return;
const outDir = api.tsconfig?.compilerOptions?.outDir;
const outFile = outDir ? file.replace(rootRegex, outDir) : file;
// Add included files from config
const includedFiles = new Set();
for (const includedFromConfig of include) {
const files = glob.sync(includedFromConfig, { cwd });
files.forEach(includedFiles.add.bind(includedFiles));
switch(event) {
case 'add':
case 'change':
fs.copyFileSync(file, outFile);
break;
case 'addDir':
fs.mkdirSync(outFile, { recursive: true });
break;
case 'unlink':
case 'unklinkDir':
fs.unlinkSync(outFile);
break;
}
},
onPostCompilation(api) {
const outDir = api.tsconfig?.compilerOptions?.outDir;
// Remove excluded files from config
for (const excludedFromConfig of exclude) {
const files = glob.sync(excludedFromConfig, { cwd });
files.forEach(includedFiles.delete.bind(includedFiles));
}
// Copy files to outDir

@@ -46,0 +66,0 @@ for (const file of includedFiles) {

@@ -7,2 +7,3 @@ const fs = require('fs');

const installDependencies = require('./utils/installDependencies');
const watchDispatcher = require('./utils/watchDispatcher');

@@ -31,27 +32,39 @@ const tsconfigDir = process.cwd();

// Create TSC Hook API
const ignoredConfigOptions = [ 'save', 'ignore', 'path', 'directory' ];
const api = {
tsconfig: {
...tsconfig,
save: function() {
const tsconfigCopy = { ...api.tsconfig };
for (const ignoredConfigOption of ignoredConfigOptions) {
eval(`delete tsconfigCopy.${ignoredConfigOption}`);
}
fs.writeFileSync(tsconfigPath, JSON.stringify(tsconfigCopy, null, 2));
// Call each hook
for (const hookModule of hookModules || []) {
// Create TSC Hook API
const ignoredConfigOptions = [ 'save', 'ignore', 'path', 'directory' ];
const watchedFiles = [];
const api = {
tsconfig: {
...tsconfig,
save() {
const tsconfigCopy = JSON.parse(JSON.stringify(api.tsconfig));
for (const ignoredConfigOption of ignoredConfigOptions) {
eval(`delete tsconfigCopy.${ignoredConfigOption}`);
}
fs.writeFileSync(tsconfigPath, JSON.stringify(tsconfigCopy, null, 2));
},
ignore(configOption) {
ignoredConfigOptions.push(configOption);
api.tsconfig.save();
},
path: tsconfigPath,
directory: tsconfigDir
},
ignore: function(configOption) {
ignoredConfigOptions.push(configOption);
api.tsconfig.save();
},
path: tsconfigPath,
directory: tsconfigDir
watch(...files) {
watchedFiles.push.apply(watchedFiles, files);
}
};
hookModule.onInitCompilation?.(api);
watchDispatcher.add(watchedFiles);
if (hookModule.onWatchCompilation) {
watchDispatcher.on('all', (event, path) => hookModule.onWatchCompilation(event, path, api));
}
};
// Call each hook
for (const hookModule of hookModules || []) {
hookModule?.onInitCompilation?.(api);
process.on('exit', () => hookModule?.onPostCompilation?.(api));
if (hookModule.onPostCompilation) {
process.on('exit', () => hookModule.onPostCompilation(api));
}
}

@@ -58,0 +71,0 @@

const { execSync } = require('child_process');
const path = require('path');
const crypto = require('crypto');
const md5 = require('./md5');

@@ -9,3 +9,3 @@ module.exports = function(hook, tsconfigDir) {

new URL(hook);
const hashPath = path.resolve(__dirname, '../hooks', `${crypto.createHash('md5').update(hook).digest('hex')}.js`);
const hashPath = path.resolve(__dirname, '../hooks', `${md5(hook)}.js`);
if (!fs.existsSync(hashPath)) {

@@ -12,0 +12,0 @@ execSync(`curl ${hook} -o ${hashPath} -s`);

{
"name": "tsc-hooks",
"version": "1.1.2-dev.20211011.2",
"version": "1.1.2-dev.20211113.1",
"description": "Add tsc compiler hooks to your TypeScript project",

@@ -24,2 +24,3 @@ "main": "run.js",

"dependencies": {
"chokidar": "^3.5.2",
"json5": "^2.2.0",

@@ -26,0 +27,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