Skip to content

Commit

Permalink
Maintenance + Cleanup
Browse files Browse the repository at this point in the history
- Added .editorconfig + Code cleanup (to style)
- Bumped dependency versions
- Added --no-audit to npm exec commands (speed improvement)
- Unlocked rollup version (Issue fixed: Swatinem/rollup-plugin-dts#64)
  • Loading branch information
nonara committed Nov 28, 2019
1 parent 66d51c6 commit af567b0
Show file tree
Hide file tree
Showing 22 changed files with 675 additions and 235 deletions.
374 changes: 374 additions & 0 deletions .editorconfig

Large diffs are not rendered by default.

40 changes: 20 additions & 20 deletions package.json
Expand Up @@ -36,54 +36,54 @@
},
"homepage": "https://github.com/nonara/ts-patch#readme",
"dependencies": {
"chalk": "^2.4.2",
"chalk": "^3.0.0",
"global-prefix": "^3.0.0",
"minimist": "^1.2.0",
"shelljs": "^0.8.3",
"strip-ansi": "^5.2.0"
"strip-ansi": "^6.0.0"
},
"bin": {
"ts-patch": "./dist/bin/cli.js"
},
"peerDependencies": {
"typescript": ">2.7.0"
"typescript": ">3.7.2"
},
"devDependencies": {
"@phenomnomnominal/tsquery": "^3.0.0",
"@types/chai": "^4.2.4",
"@types/expect": "^1.20.4",
"@phenomnomnominal/tsquery": "^4.0.0",
"@types/chai": "^4.2.5",
"@types/expect": "^24.3.0",
"@types/istanbul": "^0.4.30",
"@types/minimist": "^1.2.0",
"@types/mocha": "^5.2.7",
"@types/resolve": "0.0.8",
"@types/rewire": "^2.5.28",
"@types/shelljs": "^0.8.5",
"@types/shelljs": "^0.8.6",
"@types/sinon-chai": "^3.2.3",
"@types/test-console": "^1.1.0",
"chai": "^4.2.0",
"coveralls": "^3.0.7",
"graphql": "^14.0.2",
"graphql-tag": "^2.10.0",
"coveralls": "^3.0.9",
"graphql": "^14.5.8",
"graphql-tag": "^2.10.1",
"mocha": "^6.2.2",
"nyc": "^14.1.1",
"rewire": "^4.0.1",
"rimraf": "^3.0.0",
"rollup": "1.25.2",
"rollup-plugin-dts": "^1.1.10",
"rollup-plugin-typescript2": "^0.24.3",
"rollup": "^1.27.5",
"rollup-plugin-dts": "^1.1.12",
"rollup-plugin-typescript2": "^0.25.2",
"sinon": "^7.5.0",
"sinon-chai": "^3.3.0",
"source-map-support": "^0.5.16",
"test-console": "^1.1.0",
"ts-nameof": "^2.0.0",
"ts-node": "^8.4.1",
"ts-nameof": "^4.2.0",
"ts-node": "^8.5.4",
"ts-transform-css-modules": "^0.3.3",
"ts-transform-graphql-tag": "^0.2.1",
"ts-transform-img": "^0.3.2",
"ts-transform-react-intl": "^0.4.0",
"ts-transformer-enumerate": "^0.3.1",
"ts-transformer-keys": "^0.3.4",
"typescript": "^3.6.4"
"ts-transform-img": "^0.4.2",
"ts-transform-react-intl": "^0.4.1",
"ts-transformer-enumerate": "^0.5.1",
"ts-transformer-keys": "^0.4.1",
"typescript": "^3.7.2"
},
"directories": {
"resources": "./dist/resources"
Expand Down
14 changes: 7 additions & 7 deletions rollup.config.js
Expand Up @@ -16,18 +16,18 @@ const tsOptions = {
const config = [
{
input: 'src/patch/main.ts',
output: [{
file: path.join(pkg.directories.resources, 'module-patch.js'),
format: 'iife',
globals: [ 'ts' ]
}],
output: [ {
file: path.join(pkg.directories.resources, 'module-patch.js'),
format: 'iife',
globals: [ 'ts' ]
} ],
plugins: [ typescript(tsOptions) ]
},
{
input: 'src/patch/types.ts',
output: [{
output: [ {
file: path.join(pkg.directories.resources, 'module-patch.d.ts')
}],
} ],
plugins: [ dts(), buildPatchTypes() ]
}
];
Expand Down
4 changes: 2 additions & 2 deletions scripts/build-patch-types.js
Expand Up @@ -18,9 +18,9 @@ export default function buildPatchTypes() {
);

return omit
// Remove selected nodes from final source
// Remove selected nodes from final source
.reduce(
(p, {end}, i) => p.concat(code.slice(end, ((i + 1) < omit.length) ? omit[i + 1].pos : void 0)),
(p, { end }, i) => p.concat(code.slice(end, ((i + 1) < omit.length) ? omit[i + 1].pos : void 0)),
code.slice(0, omit[0].pos)
)
// Format for ambient context (remove 'declare')
Expand Down
60 changes: 36 additions & 24 deletions src/bin/cli.ts
Expand Up @@ -2,7 +2,7 @@ import minimist from 'minimist';
import chalk from 'chalk';
import stripAnsi from 'strip-ansi';
import * as actions from '../lib/actions'
import { parseOptions, Log, TSPOptions, appOptions, tspPackageJSON } from '../lib/system';
import { appOptions, Log, parseOptions, TSPOptions, tspPackageJSON } from '../lib/system';
import { getTSPackage } from '..';


Expand All @@ -13,28 +13,32 @@ import { getTSPackage } from '..';

type MenuData = Record<string, { short?: string, caption: string, paramCaption?: string, inverse?: boolean }>;

const cliOptions:MenuData = {
const cliOptions: MenuData = {
silent: { short: 's', caption: 'Run silently' },
global: { short: 'g', caption: 'Target global TypeScript installation' },
verbose: { short: 'v', caption: 'Chat it up' },
basedir: { short: 'd', paramCaption: '<dir>', caption: 'Base directory to resolve package from' },
persist: { caption:
persist: {
caption:
'Enable automatic persistence. (If TypeScript is updated/reinstalled, it will automatically re-patch)'
},
'no-persist': { caption: 'Disable automatic persistence.' },
color: { inverse: true, caption: 'Strip ansi colours from output' }
};

const cliCommands:MenuData = {
const cliCommands: MenuData = {
install: { short: 'i', caption: `Installs ts-patch` },
uninstall: { short: 'u', caption: 'Restores original typescript files' },
check: { short: 'c', caption:
check: {
short: 'c', caption:
`Check patch status (use with ${chalk.cyanBright('--basedir')} to specify TS package location)`
},
patch: { short: void 0, paramCaption: '<module_file> | <glob>', caption:
patch: {
short: void 0, paramCaption: '<module_file> | <glob>', caption:
'Patch specific module(s) ' + chalk.yellow('(Not recommended. Use install instead)')
},
unpatch: { short: void 0, paramCaption: '<module_file> | <glob>', caption:
unpatch: {
short: void 0, paramCaption: '<module_file> | <glob>', caption:
'Un-patch specific module(s) ' + chalk.yellow('(Not recommended. Use uninstall instead)')
},
version: { short: 'v', caption: 'Show version' },
Expand Down Expand Up @@ -64,14 +68,14 @@ const menuText =
// Commands
Object
.entries(cliCommands)
.map(([cmd, {short, caption, paramCaption}]) => formatLine([cmd, short], caption, paramCaption))
.map(([ cmd, { short, caption, paramCaption } ]) => formatLine([ cmd, short ], caption, paramCaption))
.join(LINE_INDENT) +

// Options
'\r\n' + LINE_INDENT + chalk.bold('Options') + LINE_INDENT +
Object
.entries(cliOptions)
.map(([long, {short, inverse, caption, paramCaption}]) => formatLine([
.map(([ long, { short, inverse, caption, paramCaption } ]) => formatLine([
short && `${chalk.cyanBright('-' + short)}`,
long && `${chalk.cyanBright(`${inverse ? '--no-' : '--'}${long}`)}`
], caption, paramCaption))
Expand All @@ -92,17 +96,17 @@ if (instanceIsCLI) run();

export function run() {
let args = minimist(instanceIsCLI ? process.argv.slice(2) : arguments[0].split(' '));
let cmd:string | undefined = args._[0] ? args._[0].toLowerCase() : void 0;
let cmd: string | undefined = args._[0] ? args._[0].toLowerCase() : void 0;

try {
/* Select command by short or full code */
if (cmd) cmd = (Object.keys(cliCommands).includes(cmd)) ? cmd :
(Object.entries(cliCommands).find(([n, {short}]) => n && (short == cmd)) || [])[0];
(Object.entries(cliCommands).find(([ n, { short } ]) => n && (short == cmd)) || [])[0];

/* Parse options (convert short-code to long) */
const opts = Object
.entries(cliOptions)
.reduce((p, [key, {short}]) => ({
.reduce((p, [ key, { short } ]) => ({
...p,
...(args.hasOwnProperty(key) && { [key]: args[key] }),
...(short && args.hasOwnProperty(short) && { [key]: args[short] })
Expand All @@ -119,42 +123,50 @@ export function run() {
/* Handle commands */
(() => {
switch (cmd) {
case 'help': return Log(menuText, Log.system);
case 'help':
return Log(menuText, Log.system);

case 'version':
const {version: tsVersion, packageDir} = getTSPackage(appOptions.basedir);
const { version: tsVersion, packageDir } = getTSPackage(appOptions.basedir);
return Log('\r\n' +
chalk.bold.blue('ts-patch: ') + tspPackageJSON.version + '\r\n' +
chalk.bold.blue('typescript: ') + tsVersion + chalk.gray(` [${packageDir}]`),
Log.system
);

case 'install': return actions.install();
case 'install':
return actions.install();

case 'uninstall': return actions.uninstall();
case 'uninstall':
return actions.uninstall();

case 'patch': return actions.patch(args._.slice(1).join(' '));
case 'patch':
return actions.patch(args._.slice(1).join(' '));

case 'unpatch': return actions.unpatch(args._.slice(1).join(' '));
case 'unpatch':
return actions.unpatch(args._.slice(1).join(' '));

case 'check': return actions.check();
case 'check':
return actions.check();

default: return (args.persist === undefined) ?
Log('Invalid command. Try ts-patch /? for more info', Log.system) : {}
default:
return (args.persist === undefined) ?
Log('Invalid command. Try ts-patch /? for more info', Log.system) : {}
}
})();

/* Handle persist option */
if (args.persist !== undefined) (args.persist) ? actions.enablePersistence() : actions.disablePersistence();
} catch (e) {
}
catch (e) {
Log([
'!',
chalk.bold.yellow(e.name && (e.name !== 'Error') ? `[${e.name}]: ` : 'Error: ') + chalk.red(e.message)
], Log.system);
}

// Output for analysis by tests
return (!instanceIsCLI) ? ({cmd, args, options: appOptions}) : void 0;
return (!instanceIsCLI) ? ({ cmd, args, options: appOptions }) : void 0;
}

// endregion
// endregion
7 changes: 3 additions & 4 deletions src/bin/persist.ts
Expand Up @@ -2,21 +2,20 @@

import path from 'path';
import fs from 'fs';
import { getTSPackage } from '..';
import { patch } from '..';
import { getTSPackage, patch } from '..';


/* ********************************************************************************************************************
* App
* ********************************************************************************************************************/

/* Read config file */
const {config, libDir, packageDir} = getTSPackage(path.resolve(path.dirname(process.argv[1]), '../../'));
const { config, libDir, packageDir } = getTSPackage(path.resolve(path.dirname(process.argv[1]), '../../'));

if (!config.persist) process.exit();

/* Iterate and patch updated files */
for (let [filename, timestamp] of Object.entries(config.modules)) {
for (let [ filename, timestamp ] of Object.entries(config.modules)) {
const file = path.join(libDir, filename);
if (<number>timestamp < fs.statSync(file).mtimeMs) {
try { patch(file, { silent: true, basedir: packageDir }) }
Expand Down

0 comments on commit af567b0

Please sign in to comment.