Skip to content
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.nyc_output/
artifacts/
lib/
node_modules/

Expand Down
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.nyc_output/
artifacts/
node_modules/
src/
testables/
Expand All @@ -7,6 +8,7 @@ testables/

.npmignore
.travis.yml
chest-temp.png
mocha.opts
tsconfig.json
tslint.json
Expand Down
8 changes: 8 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# License
© 2017 NativeCode Development <support@nativecode.com>

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.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# @beard/chest

<p align="center">
<img src="chest-temp.png">
<img src="https://github.com/nativecode-dev/chest/raw/master/chest-temp.png">
</p>

# WTF
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down Expand Up @@ -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",
Expand Down
16 changes: 9 additions & 7 deletions src/Chest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -35,24 +35,26 @@ 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 () => {
const directory = Files.join(process.cwd(), 'testables', 'workspaces-invalid')
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()))
const args = Object.keys(Registry.all())
Chest.run(directory, ...args).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()))
const args = Object.keys(Registry.all())
Chest.run(directory, ...args).then(() => done())
})

})
7 changes: 5 additions & 2 deletions src/Chest.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
const project = await Chest.project(root)
const projects = await Chest.projects(project)
Expand All @@ -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<NPM>(npmfile)
Expand Down
6 changes: 3 additions & 3 deletions src/Core/Actions/Packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class Script extends UpdateScript {
}

public async workspace(project: Project): Promise<void> {
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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Actions/Typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions src/Core/Files.spec.ts
Original file line number Diff line number Diff line change
@@ -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()
})

})
13 changes: 13 additions & 0 deletions src/Core/Files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ class InternalFiles {
return stats.filter(stat => stat.file).map(stat => stat.filename)
}

public mkdir(filepath: string): Promise<void> {
return new Promise<void>(async (resolve, reject) => {
fs.mkdir(filepath, (error: NodeJS.ErrnoException) => {
if (error) {
reject(error)
} else {
resolve()
}
})
})
}

public async save<T>(filepath: string, data: T): Promise<void> {
await this.writefile(filepath, JSON.stringify(data, null, 2))
}
Expand Down Expand Up @@ -116,6 +128,7 @@ export interface Files {
readfile(filepath: string): Promise<Buffer>
listdirs(filepath: string): Promise<string[]>
listfiles(filepath: string): Promise<string[]>
mkdir(filepath: string): Promise<void>
save<T>(filepath: string, data: T): Promise<void>
statfile(filepath: string): Promise<fs.Stats>
statfiles(filepath: string): Promise<Stat[]>
Expand Down
2 changes: 2 additions & 0 deletions src/Core/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 50 additions & 0 deletions src/Core/Registry.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
})

})
9 changes: 9 additions & 0 deletions src/Core/Registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void[]> {
return Promise.all(
args.map(arg => arg.toLowerCase())
Expand All @@ -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)
}
}