From bd85a4a0d2f467e1e4a228acaa09df2a19765b69 Mon Sep 17 00:00:00 2001 From: Mike Pham Date: Sun, 26 Nov 2017 20:14:25 -0500 Subject: [PATCH] fix: dependency sort and expanding typings check --- src/Core/Actions/Packages.ts | 4 +++- src/Core/Actions/Typings.spec.ts | 6 +++++- src/Core/Actions/Typings.ts | 19 +++++++------------ src/Core/Actions/Yarn.ts | 4 ++-- src/Core/Interfaces/TypeScriptOptions.ts | 5 +++++ src/Core/Interfaces/Updater.ts | 4 ++-- src/Core/Interfaces/index.ts | 1 + src/Core/Registry.ts | 3 ++- src/Core/UpdateScript.ts | 8 ++++---- testables/single/package.json | 3 ++- testables/single/tsconfig.json | 5 ++++- testables/single/yarn.lock | 4 ++++ 12 files changed, 41 insertions(+), 25 deletions(-) create mode 100644 src/Core/Interfaces/TypeScriptOptions.ts diff --git a/src/Core/Actions/Packages.ts b/src/Core/Actions/Packages.ts index b53ed44..96eb375 100644 --- a/src/Core/Actions/Packages.ts +++ b/src/Core/Actions/Packages.ts @@ -13,7 +13,7 @@ class Script extends UpdateScript { super(Script.Name, UpdaterType.Projects) } - public async workspace(project: Project): Promise { + public async workspace(project: Project): Promise { if (project.owner) { const source = await project.owner.npm const target = await project.npm @@ -35,6 +35,8 @@ class Script extends UpdateScript { this.log.task('workspace', filename) } } + + return project } } diff --git a/src/Core/Actions/Typings.spec.ts b/src/Core/Actions/Typings.spec.ts index 08ea121..e738118 100644 --- a/src/Core/Actions/Typings.spec.ts +++ b/src/Core/Actions/Typings.spec.ts @@ -1,7 +1,7 @@ import 'mocha' import { expect } from 'chai' -import { Files, Registry } from '../index' +import { Files, Registry, TypeScriptOptions } from '../index' const testables = Files.join(process.cwd(), 'testables') const single = Files.join(testables, 'single') @@ -12,11 +12,15 @@ describe('when collecting type declarations', () => { it('should execute single', () => { const script = Registry.get('typings') return script.exec(single) + .then(project => project.json('tsconfig.json')) + .then(json => expect(json.compilerOptions.types).to.contain('chalk')) }) it('should execute workspaces', () => { const script = Registry.get('typings') return script.exec(workspaces) + .then(project => project.json('tsconfig.json')) + .then(json => expect(json.compilerOptions.types).to.contain('chalk')) }) }) diff --git a/src/Core/Actions/Typings.ts b/src/Core/Actions/Typings.ts index cdff02b..145a007 100644 --- a/src/Core/Actions/Typings.ts +++ b/src/Core/Actions/Typings.ts @@ -1,9 +1,4 @@ -import { CompilerOptions } from 'typescript' -import { Files, NPM, Project, Registry, UpdateScript, UpdaterType } from '../index' - -interface TsConfig { - compilerOptions: CompilerOptions -} +import { Files, NPM, Project, Registry, TypeScriptOptions, UpdateScript, UpdaterType } from '../index' /* * Updates the "types" property of "tsconfig.json" files by @@ -16,13 +11,13 @@ class Script extends UpdateScript { super(Script.Name, UpdaterType.Root) } - public exec(rootpath: string): Promise { + public exec(rootpath: string): Promise { return Project.load(rootpath) .then(project => this.declarations([project, ...project.children]) .then(typings => this.typings(project, typings)) + .then(() => project) ) - .then(() => void (0)) } private declarations(projects: Project[]): Promise { @@ -41,12 +36,12 @@ class Script extends UpdateScript { private typings(project: Project, typings: string[]): Promise { return Promise.all(typings.map(typing => Files.join(project.path, 'node_modules', typing, 'package.json'))) .then(typings => Promise.all(typings.map(typing => Files.json(typing)))) - .then(npms => npms.filter(npm => npm.types || npm.typings).map(npm => npm.name)) - .then(typings => project.json('tsconfig.json').then(tsconfig => { - tsconfig.compilerOptions.types = typings + .then(npms => npms.filter(npm => npm.types || npm.typings || npm.typeScriptVersion).map(npm => npm.name)) + .then(typings => project.json('tsconfig.json').then(tsconfig => { + tsconfig.compilerOptions.types = typings.sort() return tsconfig })) - .then(tsconfig => this.testing ? Promise.resolve() : Files.save('tsconfig.json', tsconfig)) + .then(tsconfig => project.save('tsconfig.json', tsconfig)) } } diff --git a/src/Core/Actions/Yarn.ts b/src/Core/Actions/Yarn.ts index 15f0838..c0870d2 100644 --- a/src/Core/Actions/Yarn.ts +++ b/src/Core/Actions/Yarn.ts @@ -10,8 +10,8 @@ export class Yarn extends UpdateScript { super(Yarn.Name, UpdaterType.Root) } - public exec(rootpath: string): Promise { - return Project.load(rootpath).then(project => this.run(project, 'yarn')) + public exec(rootpath: string): Promise { + return Project.load(rootpath).then(project => this.run(project, 'yarn').then(() => project)) } } diff --git a/src/Core/Interfaces/TypeScriptOptions.ts b/src/Core/Interfaces/TypeScriptOptions.ts new file mode 100644 index 0000000..2653007 --- /dev/null +++ b/src/Core/Interfaces/TypeScriptOptions.ts @@ -0,0 +1,5 @@ +import { CompilerOptions } from 'typescript' + +export interface TypeScriptOptions { + compilerOptions: CompilerOptions +} diff --git a/src/Core/Interfaces/Updater.ts b/src/Core/Interfaces/Updater.ts index e689a44..05ce284 100644 --- a/src/Core/Interfaces/Updater.ts +++ b/src/Core/Interfaces/Updater.ts @@ -4,8 +4,8 @@ import { Project } from '../Project' export interface Updater { name: string type: UpdaterType - exec(rootpath: string): Promise - workspace(project: Project): Promise + exec(rootpath: string): Promise + workspace(project: Project): Promise } export type Updaters = { diff --git a/src/Core/Interfaces/index.ts b/src/Core/Interfaces/index.ts index 69a9bba..74d97b0 100644 --- a/src/Core/Interfaces/index.ts +++ b/src/Core/Interfaces/index.ts @@ -1,4 +1,5 @@ export * from './Dictionary' export * from './NPM' +export * from './TypeScriptOptions' export * from './Updater' export * from './UpdaterType' diff --git a/src/Core/Registry.ts b/src/Core/Registry.ts index 90ca84b..74873cb 100644 --- a/src/Core/Registry.ts +++ b/src/Core/Registry.ts @@ -1,4 +1,5 @@ import { Dictionary, Updater, Updaters } from './Interfaces' +import { Project } from './Project' export class Registry { private static readonly registrations: Updaters = {} @@ -15,7 +16,7 @@ export class Registry { return !!this.registrations[name.toLowerCase()] } - public static execute(root: string, ...args: string[]): Promise { + public static execute(root: string, ...args: string[]): Promise { return Promise.all(args.map(arg => arg.toLowerCase()).map(name => this.registrations[name].exec(root))) } diff --git a/src/Core/UpdateScript.ts b/src/Core/UpdateScript.ts index 06119f5..8525361 100644 --- a/src/Core/UpdateScript.ts +++ b/src/Core/UpdateScript.ts @@ -27,12 +27,12 @@ export abstract class UpdateScript implements Updater { return this._type } - public exec(rootpath: string): Promise { - return Promise.resolve() + public exec(rootpath: string): Promise { + return Promise.resolve(Project.InvalidProject) } - public workspace(project: Project): Promise { - return Promise.resolve() + public workspace(project: Project): Promise { + return Promise.resolve(Project.InvalidProject) } protected npm(basepath: string): Promise { diff --git a/testables/single/package.json b/testables/single/package.json index 50abe9e..cfbd70e 100644 --- a/testables/single/package.json +++ b/testables/single/package.json @@ -4,7 +4,8 @@ }, "description": "single project", "devDependencies": { - "@types/chalk": "^2.2.0" + "@types/chalk": "^2.2.0", + "@types/node": "^8.0.52" }, "name": "project-single", "version": "1.0.0" diff --git a/testables/single/tsconfig.json b/testables/single/tsconfig.json index b798bc0..fa38149 100644 --- a/testables/single/tsconfig.json +++ b/testables/single/tsconfig.json @@ -1,5 +1,8 @@ { "compilerOptions": { - "types": [] + "types": [ + "@types/node", + "chalk" + ] } } \ No newline at end of file diff --git a/testables/single/yarn.lock b/testables/single/yarn.lock index 4ab6260..018ae30 100644 --- a/testables/single/yarn.lock +++ b/testables/single/yarn.lock @@ -8,6 +8,10 @@ dependencies: chalk "*" +"@types/node@^8.0.52": + version "8.0.53" + resolved "https://registry.yarnpkg.com/@types/node/-/node-8.0.53.tgz#396b35af826fa66aad472c8cb7b8d5e277f4e6d8" + ansi-styles@^3.1.0: version "3.2.0" resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.2.0.tgz#c159b8d5be0f9e5a6f346dab94f16ce022161b88"