Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,20 @@
"README.md"
],
"devDependencies": {
"@npm/types": "^1.0.1",
"@types/commander": "^2.12.2",
"@types/jest": "^23.3.10",
"@types/node": "^10.12.18",
"@types/node-fetch": "^2.1.2",
"@types/npm-registry-fetch": "^4.0.1",
"@types/npmlog": "^4.1.1",
"jest": "^23.6.0",
"ts-jest": "^23.10.5",
"tslint": "^5.12.0",
"typescript": "^3.2.2"
},
"dependencies": {
"commander": "^2.19.0"
"commander": "^2.19.0",
"npm-registry-fetch": "^3.8.0"
}
}
59 changes: 45 additions & 14 deletions src/typac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import * as fs from 'fs';
import * as path from 'path';
import * as programm from 'commander';
import { spawnSync, SpawnSyncOptions } from 'child_process';
import * as npmFetch from 'npm-registry-fetch';
import * as npm from '@npm/types';
import { typifyPackageName, installArguments, PackageManager } from './utils';

const pckg = require('../package.json');
Expand All @@ -17,6 +19,8 @@ const useYarn = fs.existsSync(path.join(process.cwd(), 'yarn.lock'));
programm
.version(pckg.version)
.usage('[options] package_name [package_name_1 ...]')
.option('-D, --dev') // alias
.option('-S, --save') // alias
.option('-d, --dev', 'save all to devDependencies')
.option('-s, --save', 'save all to dependencies')
.parse(process.argv);
Expand All @@ -31,22 +35,49 @@ if (dev && save) {
process.exit(1);
}

const manager = useYarn ? PackageManager.YARN : PackageManager.NPM;
main().catch((err) => {
throw err;
});
async function main() {
const manager = useYarn ? PackageManager.YARN : PackageManager.NPM;

const args = installArguments(manager, packages, !dev, dev);
const argsTyped = installArguments(manager, packages.map((p) => typifyPackageName(p)), save, !save);
const typedPackages = (await Promise.all(packages
.map(typifyPackageName)
.map(async (p) => {
try {
const json = await npmFetch.json(p.replace(/\//g, '%2F')) as unknown as npm.Manifest;
const latestVersion = json['dist-tags'].latest;
if (json.versions[latestVersion].deprecated) {
return false;
}
return json.name;
} catch (err) {
if (err.statusCode !== 404) {
throw err;
}
return false;
}
})))
.filter(<T>(p: T | false): p is T => Boolean(p));

const command = manager + ' ' + args.join(' ');
const commandTyped = manager + ' ' + argsTyped.join(' ');
const args = installArguments(manager, packages, !dev, dev);
const argsTyped = installArguments(manager, typedPackages, save, !save);

console.log('installing', packages.length, `package${packages.length > 1 ? 's' : ''} with`, useYarn ? 'yarn' : 'npm');
const command = manager + ' ' + args.join(' ');
const commandTyped = manager + ' ' + argsTyped.join(' ');
console.log('installing', packages.length, `package${packages.length > 1 ? 's' : ''} with`, useYarn ? 'yarn' : 'npm');

const spawnParams: SpawnSyncOptions = {
stdio: 'inherit',
cwd: process.cwd()
};
const spawnParams: SpawnSyncOptions = {
stdio: 'inherit',
cwd: process.cwd()
};

console.log('running', command);
spawnSync(manager, args, spawnParams);
console.log('running', commandTyped);
spawnSync(manager, argsTyped, spawnParams);
console.log('running', command);
spawnSync(manager, args, spawnParams);
if (typedPackages.length > 0) {
console.log('running', commandTyped);
spawnSync(manager, argsTyped, spawnParams);
} else {
console.log('no @types module found for requested packages');
}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
"build",
"tests"
]
}
}
Loading