From 0fb9e84990f4c63a7271204fb715321a2aee8c3f Mon Sep 17 00:00:00 2001 From: Mike Pham Date: Wed, 15 Nov 2017 19:20:03 -0500 Subject: [PATCH 1/3] adding license file and removing image from published --- .npmignore | 1 + LICENSE | 8 ++++++++ README.md | 2 +- 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 LICENSE diff --git a/.npmignore b/.npmignore index 3874581..a7c754d 100644 --- a/.npmignore +++ b/.npmignore @@ -7,6 +7,7 @@ testables/ .npmignore .travis.yml +chest-temp.png mocha.opts tsconfig.json tslint.json diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9fc972a --- /dev/null +++ b/LICENSE @@ -0,0 +1,8 @@ +# License +© 2017 NativeCode Development + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +> The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index afd9d74..78b31e9 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ # @beard/chest

- +

# WTF From 065a4237d2b58859483cf793f61765ed496fbb0c Mon Sep 17 00:00:00 2001 From: Mike Pham Date: Wed, 15 Nov 2017 19:27:38 -0500 Subject: [PATCH 2/3] fixing test --- package.json | 2 +- src/Chest.spec.ts | 8 ++++---- src/Core/Actions/Packages.ts | 6 +++--- src/Core/Actions/Typings.ts | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 019af49..63a868a 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "license": "MIT", "main": "./lib/CLI.js", "typings": "./lib/index.d.ts", - "version": "2.0.0-alpha-3", + "version": "2.0.0-alpha-4", "dependencies": { "chalk": "^2.3.0" }, diff --git a/src/Chest.spec.ts b/src/Chest.spec.ts index 150ad6f..d9545c8 100644 --- a/src/Chest.spec.ts +++ b/src/Chest.spec.ts @@ -45,14 +45,14 @@ describe('when using RootProject to load a project', () => { Chest.project(directory).then(project => Chest.projects(project).should.eventually.throw()) }) - it('should run scripts for single project', () => { + it('should run scripts for single project', (done) => { const directory = Files.join(process.cwd(), 'testables', 'single') - Chest.run(directory, ...Object.keys(Registry.all())) + Chest.run(directory, ...Object.keys(Registry.all())).then(() => done()) }) - it('should run scripts for workspace project', () => { + it('should run scripts for workspace project', (done) => { const directory = Files.join(process.cwd(), 'testables', 'workspaces') - Chest.run(directory, ...Object.keys(Registry.all())) + Chest.run(directory, ...Object.keys(Registry.all())).then(() => done()) }) }) diff --git a/src/Core/Actions/Packages.ts b/src/Core/Actions/Packages.ts index 3e47a53..84173ce 100644 --- a/src/Core/Actions/Packages.ts +++ b/src/Core/Actions/Packages.ts @@ -24,8 +24,8 @@ class Script extends UpdateScript { } public async workspace(project: Project): Promise { - const source = await project.package - const target = await project.owner.package + const source = await project.owner.package + const target = await project.package target.author = source.author target.bugs = source.bugs @@ -37,7 +37,7 @@ class Script extends UpdateScript { const filename = path.join(project.path, 'package.json') if (this.testing) { - this.log.task('updated package info', filename, target) + this.log.task('updated package info', filename, JSON.stringify(target, null, 2)) } else { await Files.save(filename, target) this.log.task('updated package info', filename) diff --git a/src/Core/Actions/Typings.ts b/src/Core/Actions/Typings.ts index 0b566fb..ddaeb8d 100644 --- a/src/Core/Actions/Typings.ts +++ b/src/Core/Actions/Typings.ts @@ -36,7 +36,7 @@ class Script extends UpdateScript { tsconfig.compilerOptions.types = typings.map(typing => typing.npmname).sort() if (this.testing) { - this.log.task('updated types', tsconfigfile, tsconfig) + this.log.task('updated types', tsconfigfile, JSON.stringify(tsconfig, null, 2)) } else { await Files.save(tsconfigfile, tsconfig) this.log.task('updated types', tsconfigfile) From 28ab1f92a2d4a45aeafe906f2e0acb292fa32db8 Mon Sep 17 00:00:00 2001 From: Mike Pham Date: Wed, 15 Nov 2017 21:06:13 -0500 Subject: [PATCH 3/3] adding tests --- .gitignore | 1 + .npmignore | 1 + package.json | 2 +- src/Chest.spec.ts | 12 ++++++---- src/Chest.ts | 7 ++++-- src/Core/Files.spec.ts | 27 +++++++++++++++++++++ src/Core/Files.ts | 13 ++++++++++ src/Core/Project.ts | 2 ++ src/Core/Registry.spec.ts | 50 +++++++++++++++++++++++++++++++++++++++ src/Core/Registry.ts | 9 +++++++ 10 files changed, 116 insertions(+), 8 deletions(-) create mode 100644 src/Core/Files.spec.ts create mode 100644 src/Core/Registry.spec.ts diff --git a/.gitignore b/.gitignore index 7ff4843..5631318 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .nyc_output/ +artifacts/ lib/ node_modules/ diff --git a/.npmignore b/.npmignore index a7c754d..1335f3d 100644 --- a/.npmignore +++ b/.npmignore @@ -1,4 +1,5 @@ .nyc_output/ +artifacts/ node_modules/ src/ testables/ diff --git a/package.json b/package.json index 63a868a..caf6ac5 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ "scripts": { "build": "tsc --project tsconfig.json", "lint": "tslint src/**/*.ts", - "prebuild": "rimraf lib", + "prebuild": "rimraf lib && rimraf artifacts", "postbuild": "bin-shebang && yarn run lint", "test": "cross-env NODE_ENV=test nyc mocha --opts mocha.opts", "pretest": "yarn run build", diff --git a/src/Chest.spec.ts b/src/Chest.spec.ts index d9545c8..0583953 100644 --- a/src/Chest.spec.ts +++ b/src/Chest.spec.ts @@ -4,7 +4,7 @@ import * as chai from 'chai' import * as chaiAsPromised from 'chai-as-promised' import { Chest } from './Chest' -import { Files, Registry } from './Core' +import { Files, Project, Registry } from './Core' const expect = chai.expect @@ -35,9 +35,9 @@ describe('when using RootProject to load a project', () => { expect(projects[0].owner).to.not.equal(undefined) }) - it('should throw error when single project does not exist', async () => { + it('should return static InvalidProject when single project does not exist', async () => { const directory = Files.join(process.cwd(), 'testables', 'nonexistant') - Chest.project(directory).then(project => Chest.projects(project).should.eventually.throw()) + Chest.project(directory).then(project => Chest.projects(project).should.eventually.equal(Project.InvalidProject)) }) it('should throw error when workspace project has no child projects', async () => { @@ -47,12 +47,14 @@ describe('when using RootProject to load a project', () => { it('should run scripts for single project', (done) => { const directory = Files.join(process.cwd(), 'testables', 'single') - Chest.run(directory, ...Object.keys(Registry.all())).then(() => done()) + const args = Object.keys(Registry.all()) + Chest.run(directory, ...args).then(() => done()) }) it('should run scripts for workspace project', (done) => { const directory = Files.join(process.cwd(), 'testables', 'workspaces') - Chest.run(directory, ...Object.keys(Registry.all())).then(() => done()) + const args = Object.keys(Registry.all()) + Chest.run(directory, ...args).then(() => done()) }) }) diff --git a/src/Chest.ts b/src/Chest.ts index cece83b..bb8de3d 100644 --- a/src/Chest.ts +++ b/src/Chest.ts @@ -1,8 +1,10 @@ import * as path from 'path' -import { Files, NPM, Project, Registry, UpdaterType } from './Core' +import { Files, Log, Logger, NPM, Project, Registry, UpdaterType } from './Core' export class Chest { + private static readonly Log: Log = Logger('chest') + public static async run(root: string, ...args: string[]): Promise { const project = await Chest.project(root) const projects = await Chest.projects(project) @@ -23,7 +25,8 @@ export class Chest { const npmfile = path.join(root, 'package.json') if (await Files.exists(npmfile) === false) { - throw new Error(`failed to find ${npmfile} in ${root}`) + Chest.Log.error(new Error(`failed to find ${npmfile} in ${root}`)) + return Project.InvalidProject } const npm = await Files.json(npmfile) diff --git a/src/Core/Files.spec.ts b/src/Core/Files.spec.ts new file mode 100644 index 0000000..e652c53 --- /dev/null +++ b/src/Core/Files.spec.ts @@ -0,0 +1,27 @@ +import 'mocha' + +import * as chai from 'chai' +import * as chaiAsPromise from 'chai-as-promised' + +import { Files } from './Files' + +const expect = chai.expect + +describe('', () => { + + before(async () => { + const artifacts = Files.join(process.cwd(), 'artifacts') + await Files.mkdir(artifacts) + }) + + beforeEach(() => { + chai.should() + chai.use(chaiAsPromise) + }) + + it('should write file', () => { + const filename = Files.join(process.cwd(), 'artifacts', 'test.json') + Files.writefile(filename, {}).should.eventually.not.throw() + }) + +}) diff --git a/src/Core/Files.ts b/src/Core/Files.ts index cce268c..dcc43d3 100644 --- a/src/Core/Files.ts +++ b/src/Core/Files.ts @@ -55,6 +55,18 @@ class InternalFiles { return stats.filter(stat => stat.file).map(stat => stat.filename) } + public mkdir(filepath: string): Promise { + return new Promise(async (resolve, reject) => { + fs.mkdir(filepath, (error: NodeJS.ErrnoException) => { + if (error) { + reject(error) + } else { + resolve() + } + }) + }) + } + public async save(filepath: string, data: T): Promise { await this.writefile(filepath, JSON.stringify(data, null, 2)) } @@ -116,6 +128,7 @@ export interface Files { readfile(filepath: string): Promise listdirs(filepath: string): Promise listfiles(filepath: string): Promise + mkdir(filepath: string): Promise save(filepath: string, data: T): Promise statfile(filepath: string): Promise statfiles(filepath: string): Promise diff --git a/src/Core/Project.ts b/src/Core/Project.ts index 32b0137..5d0a521 100644 --- a/src/Core/Project.ts +++ b/src/Core/Project.ts @@ -2,6 +2,8 @@ import { Files } from './Files' import { NPM } from './Interfaces' export class Project { + public static InvalidProject: Project = new Project('invalid', 'invalid') + private readonly _name: string private readonly _owner: Project private readonly _path: string diff --git a/src/Core/Registry.spec.ts b/src/Core/Registry.spec.ts new file mode 100644 index 0000000..b6bd4ad --- /dev/null +++ b/src/Core/Registry.spec.ts @@ -0,0 +1,50 @@ +import 'mocha' + +import * as chai from 'chai' +import * as chaiAsPromised from 'chai-as-promised' + +import { Files } from './Files' +import { UpdaterType } from './Interfaces' +import { Registry } from './Registry' +import { UpdateScript } from './UpdateScript' + +const expect = chai.expect +const ScriptName = Files.extensionless(__filename) +const RootScriptName = `${ScriptName}-root` +const ProjectsScriptName = `${ScriptName}-projects` + +class DoesNothingGoesNowhereRoot extends UpdateScript { + constructor() { + super(RootScriptName, UpdaterType.Root) + } +} + +class DoesNothingGoesNowhereProjects extends UpdateScript { + constructor() { + super(ProjectsScriptName, UpdaterType.Projects) + } +} + +describe('', () => { + + beforeEach(() => { + chai.should() + chai.use(chaiAsPromised) + }) + + it('should return registered script names', () => { + expect(Registry.names().length).to.be.gt(0) + }) + + it('should register new script object', () => { + expect(Registry.contains(RootScriptName)).to.equal(false) + Registry.add(RootScriptName, new DoesNothingGoesNowhereRoot()) + expect(Registry.contains(RootScriptName)).to.equal(true) + expect(Registry.get(RootScriptName).name).to.equal(RootScriptName) + }) + + it('should throw error when calling "get" and script does not exist', () => { + expect(() => Registry.get('invalid')).to.throw(Error) + }) + +}) diff --git a/src/Core/Registry.ts b/src/Core/Registry.ts index a4606c6..cf4fcf3 100644 --- a/src/Core/Registry.ts +++ b/src/Core/Registry.ts @@ -11,6 +11,10 @@ export class Registry { return Object.assign({}, this.registrations) } + public static contains(name: string): boolean { + return this.registrations[name.toLowerCase()] !== undefined + } + public static execute(root: string, ...args: string[]): Promise { return Promise.all( args.map(arg => arg.toLowerCase()) @@ -20,10 +24,15 @@ export class Registry { public static get(name: string): Updater { const key = name.toLowerCase() + if (this.registrations[key]) { return this.registrations[key] } throw new Error(`no registered updaters named ${name}`) } + + public static names(): string[] { + return Object.keys(this.registrations) + } }