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
12 changes: 8 additions & 4 deletions src/Core/Files.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ describe('when working with files', () => {
const invalidFile = Files.join(artifacts, 'invalid', 'invalid.json')
const nonexistant = Files.join(process.cwd(), 'nonexistant')
const testables = Files.join(process.cwd(), 'testables')
const invalid = Files.join(testables, 'workspaces-invalid')
const lerna = Files.join(testables, 'workspaces-lerna')
const single = Files.join(testables, 'single')
const workspaces = Files.join(testables, 'workspaces')

before(async () => {
Expand All @@ -27,10 +30,11 @@ describe('when working with files', () => {

it('should list directories', async () => {
const directories = await Files.listdirs(testables)
expect(directories.length).to.equal(3)
expect(directories).to.contain(Files.join(process.cwd(), 'testables/single'))
expect(directories).to.contain(Files.join(process.cwd(), 'testables/workspaces'))
expect(directories).to.contain(Files.join(process.cwd(), 'testables/workspaces-invalid'))
expect(directories.length).to.equal(4)
expect(directories).to.contain(lerna)
expect(directories).to.contain(invalid)
expect(directories).to.contain(single)
expect(directories).to.contain(workspaces)
})

it('should throw error when listing directories', (done) => {
Expand Down
9 changes: 9 additions & 0 deletions src/Core/Project.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const expect = chai.expect

const testables = Files.join(process.cwd(), 'testables')
const single = Files.join(testables, 'single')
const lerna = Files.join(testables, 'workspaces-lerna')
const workspaces = Files.join(testables, 'workspaces')

const TIMEOUT = 10000
Expand All @@ -29,6 +30,14 @@ describe('when loading projects', () => {
})
}).timeout(TIMEOUT)

it('should load yarn lerna project', () => {
return Project.load(lerna).then(project => {
expect(project.children.length).to.equal(2)
expect(project.children[0].owner).to.not.equal(undefined)
expect(project.children[1].owner).to.not.equal(undefined)
})
}).timeout(TIMEOUT)

it('should load yarn workspace project', () => {
return Project.load(workspaces).then(project => {
expect(project.children.length).to.equal(2)
Expand Down
48 changes: 8 additions & 40 deletions src/Core/Project.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Files } from './Files'
import { LernaConfig, NPM } from './Interfaces'
import { NPM } from './Interfaces'
import { Log, Logger } from './Logger'

export class Project {
Expand Down Expand Up @@ -40,22 +40,10 @@ export class Project {
return this._path
}

public static async load(rootpath: string): Promise<Project> {
const npmfile = Files.join(rootpath, 'package.json')

if (await Files.exists(npmfile) === false) {
return Project.InvalidProject
}

const npm = await Files.json<NPM>(npmfile)
const project = new Project(npm.name, rootpath)

if (npm.private && npm.workspaces) {
const result = await project.loadChildProjects(npm)
return result
}

return project
public static load(rootpath: string): Promise<Project> {
return Files.json<NPM>(Files.join(rootpath, 'package.json'))
.then(npm => ({ npm, project: new Project(npm.name, rootpath) }))
.then(load => load.project.workspaces(load.npm).then(() => load.project))
}

public json<T>(filename: string): Promise<T> {
Expand All @@ -66,31 +54,11 @@ export class Project {
return Files.save(Files.join(this.path, filename), data)
}

private async loadChildProjects(npm: NPM): Promise<Project> {
const lernafile = Files.join(this.path, 'lerna.json')
if (await Files.exists(lernafile)) {
this.log.debug('lerna-packages', this.path)
return this.loadLernaPackages(lernafile, this)
}
this.log.debug('yarn-workspaces', this.path)
return this.loadYarnWorkspaces(this)
}

private async loadLernaPackages(filepath: string, project: Project): Promise<Project> {
const lerna = await Files.json<LernaConfig>(filepath)
if (lerna.packages && lerna.useWorkspaces) {
lerna.packages.forEach(async workspace => {
const workspaceName = workspace.substring(0, workspace.indexOf('/*'))
const workspacePath = Files.join(project.path, workspaceName)
const children = await this.loadProjects(workspacePath)
children.forEach(child => this.children.push(child))
this.log.debug('lerna-package', workspaceName)
})
}
return this
private workspaces(npm: NPM): Promise<Project> {
return this.yarn(this)
}

private async loadYarnWorkspaces(project: Project): Promise<Project> {
private async yarn(project: Project): Promise<Project> {
const npm = await this.npm
if (npm.workspaces) {
return Promise.all(npm.workspaces.map(async workspace => {
Expand Down
1 change: 1 addition & 0 deletions testables/workspaces-lerna/.yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
workspaces-experimental true
10 changes: 10 additions & 0 deletions testables/workspaces-lerna/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"description": "project with workspaces",
"name": "project-workspaces",
"private": true,
"workspaces": [
"packages/*",
"projects/*"
],
"version": "1.0.0"
}
12 changes: 12 additions & 0 deletions testables/workspaces-lerna/packages/simple-package/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"dependencies": {
"chalk": "^2.3.0"
},
"description": "project with workspaces",
"devDependencies": {
"@types/chalk": "^2.2.0",
"@types/node": "^8.0.52"
},
"name": "simple-package",
"version": "1.0.0"
}
12 changes: 12 additions & 0 deletions testables/workspaces-lerna/projects/simple-project/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"dependencies": {
"chalk": "^2.3.0"
},
"description": "project with workspaces",
"devDependencies": {
"@types/chalk": "^2.2.0",
"@types/node": "^8.0.52"
},
"name": "simple-project",
"version": "1.0.0"
}
8 changes: 8 additions & 0 deletions testables/workspaces-lerna/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"types": [
"@types/node",
"chalk"
]
}
}
51 changes: 51 additions & 0 deletions testables/workspaces-lerna/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"@types/chalk@^2.2.0":
version "2.2.0"
resolved "https://registry.yarnpkg.com/@types/chalk/-/chalk-2.2.0.tgz#b7f6e446f4511029ee8e3f43075fb5b73fbaa0ba"
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"
dependencies:
color-convert "^1.9.0"

chalk@*, chalk@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.3.0.tgz#b5ea48efc9c1793dccc9b4767c93914d3f2d52ba"
dependencies:
ansi-styles "^3.1.0"
escape-string-regexp "^1.0.5"
supports-color "^4.0.0"

color-convert@^1.9.0:
version "1.9.1"
resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-1.9.1.tgz#c1261107aeb2f294ebffec9ed9ecad529a6097ed"
dependencies:
color-name "^1.1.1"

color-name@^1.1.1:
version "1.1.3"
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.3.tgz#a7d0558bd89c42f795dd42328f740831ca53bc25"

escape-string-regexp@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"

has-flag@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-2.0.0.tgz#e8207af1cc7b30d446cc70b734b5e8be18f88d51"

supports-color@^4.0.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-4.5.0.tgz#be7a0de484dec5c5cddf8b3d59125044912f635b"
dependencies:
has-flag "^2.0.0"