This repository has been archived by the owner on Mar 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from primer/alias-rules
Support path aliases via rules.json
- Loading branch information
Showing
14 changed files
with
265 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
const mockedEnv = require('mocked-env') | ||
const readJSON = require('../read-json') | ||
const deploy = require('../deploy') | ||
const now = require('../now') | ||
const commitStatus = require('../commit-status') | ||
|
||
jest.mock('../now') | ||
jest.mock('../read-json') | ||
jest.mock('../commit-status') | ||
|
||
commitStatus.mockImplementation(() => Promise.resolve({})) | ||
|
||
describe('deploy()', () => { | ||
let restoreEnv = () => {} | ||
afterEach(() => { | ||
restoreEnv() | ||
// clear the call data | ||
now.mockReset() | ||
// restore the original behavior | ||
readJSON.mockReset() | ||
}) | ||
|
||
it('calls now() once when w/o an alias', () => { | ||
mockFiles({ | ||
'package.json': {name: 'foo'}, | ||
'now.json': {}, | ||
'rules.json': null | ||
}) | ||
|
||
const url = 'foo-123.now.sh' | ||
now.mockImplementation(() => Promise.resolve(url)) | ||
mockEnv({GITHUB_REF: ''}) | ||
|
||
return deploy().then(res => { | ||
expect(now).toHaveBeenCalledTimes(1) | ||
expect(now).toHaveBeenCalledWith() | ||
expect(res).toEqual({name: 'foo', root: url, url}) | ||
}) | ||
}) | ||
|
||
it('deploys to a branch alias on branches other than master', () => { | ||
mockFiles({ | ||
'package.json': {name: 'foo'}, | ||
'now.json': {}, | ||
'rules.json': null | ||
}) | ||
|
||
const url = 'foo-123.now.sh' | ||
const alias = 'foo-bar.now.sh' | ||
now.mockImplementation(() => Promise.resolve(url)) | ||
mockEnv({GITHUB_REF: 'refs/heads/bar'}) | ||
|
||
return deploy().then(res => { | ||
expect(now).toHaveBeenCalledTimes(2) | ||
expect(now).toHaveBeenNthCalledWith(1) | ||
expect(now).toHaveBeenNthCalledWith(2, ['alias', url, alias]) | ||
expect(res).toEqual({name: 'foo', root: url, alias, url: alias}) | ||
}) | ||
}) | ||
|
||
it('deploys to the "alias" field from now.json on the master branch', () => { | ||
const name = 'hello' | ||
const alias = 'hello.world' | ||
mockFiles({ | ||
'package.json': {name}, | ||
'now.json': {alias}, | ||
'rules.json': null | ||
}) | ||
|
||
const root = 'hello-123.now.sh' | ||
now.mockImplementation(() => Promise.resolve(root)) | ||
mockEnv({GITHUB_REF: 'refs/heads/master'}) | ||
|
||
return deploy().then(res => { | ||
expect(now).toHaveBeenCalledTimes(2) | ||
expect(now).toHaveBeenNthCalledWith(1) | ||
expect(now).toHaveBeenNthCalledWith(2, ['alias', root, alias]) | ||
expect(res).toEqual({name, root, url: alias}) | ||
}) | ||
}) | ||
|
||
it('calls now() two times when there is a rules.json, not on master', () => { | ||
const prodAlias = 'foo.now.sh' | ||
mockFiles({ | ||
'package.json': {name: 'foo'}, | ||
'now.json': {alias: prodAlias}, | ||
// exists | ||
'rules.json': {} | ||
}) | ||
|
||
const url = 'foo-123.now.sh' | ||
const alias = 'foo-bar.now.sh' | ||
now.mockImplementation(() => Promise.resolve(url)) | ||
mockEnv({GITHUB_REF: 'refs/heads/bar'}) | ||
|
||
return deploy().then(res => { | ||
expect(now).toHaveBeenCalledTimes(2) | ||
expect(now).toHaveBeenNthCalledWith(1) | ||
expect(now).toHaveBeenNthCalledWith(2, ['alias', url, alias]) | ||
expect(res).toEqual({name: 'foo', root: url, alias, url: alias}) | ||
}) | ||
}) | ||
|
||
it('calls now() three times when there is a rules.json, on master', () => { | ||
const prodAlias = 'primer.style' | ||
mockFiles({ | ||
'package.json': {name: 'primer-style'}, | ||
'now.json': {alias: prodAlias}, | ||
// exists | ||
'rules.json': {} | ||
}) | ||
|
||
const url = 'primer-style-123.now.sh' | ||
const alias = 'primer-style.now.sh' | ||
now.mockImplementation(() => Promise.resolve(url)) | ||
mockEnv({GITHUB_REF: 'refs/heads/master'}) | ||
|
||
return deploy().then(res => { | ||
expect(now).toHaveBeenCalledTimes(3) | ||
expect(now).toHaveBeenNthCalledWith(1) | ||
expect(now).toHaveBeenNthCalledWith(2, ['alias', url, alias]) | ||
expect(now).toHaveBeenNthCalledWith(3, ['alias', alias, prodAlias, '-r', 'rules.json']) | ||
expect(res).toEqual({name: 'primer-style', root: url, alias, url: prodAlias}) | ||
}) | ||
}) | ||
|
||
function mockEnv(env) { | ||
restoreEnv = mockedEnv(env) | ||
} | ||
|
||
function mockFiles(files) { | ||
readJSON.mockImplementation(path => { | ||
if (path in files) { | ||
return files[path] | ||
} | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
const mockedEnv = require('mocked-env') | ||
const getAlias = require('../get-alias') | ||
|
||
describe('getAlias()', () => { | ||
it('works', () => { | ||
const restore = mockedEnv({GITHUB_REF: 'refs/heads/add-foo'}) | ||
expect(getAlias('@primer/css')).toEqual('primer-css-add-foo.now.sh') | ||
restore() | ||
expect(getAlias('@primer/css', 'add-foo')).toEqual('primer-css-add-foo.now.sh') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const execa = require('execa') | ||
const mockedEnv = require('mocked-env') | ||
const now = require('../now') | ||
|
||
jest.mock('execa') | ||
execa.mockImplementation(() => Promise.resolve({stderr: '', stdout: ''})) | ||
|
||
describe('now()', () => { | ||
let restoreEnv = () => {} | ||
afterEach(() => restoreEnv()) | ||
|
||
it('throws if process.env.NOW_TOKEN is falsy', () => { | ||
mockEnv({NOW_TOKEN: ''}) | ||
expect(() => now()).toThrow() | ||
}) | ||
|
||
it('calls `npx now --token=$NOW_TOKEN` with no additional args', () => { | ||
mockEnv({NOW_TOKEN: 'xyz'}) | ||
now() | ||
expect(execa).lastCalledWith('npx', ['now', '--token=xyz'], {stderr: 'inherit'}) | ||
}) | ||
|
||
it('calls with additional arguments passed as an array', () => { | ||
mockEnv({NOW_TOKEN: 'abc'}) | ||
now(['foo', 'bar']) | ||
expect(execa).lastCalledWith('npx', ['now', '--token=abc', 'foo', 'bar'], {stderr: 'inherit'}) | ||
}) | ||
|
||
function mockEnv(env) { | ||
restoreEnv = mockedEnv(env) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const readJSON = require('../read-json') | ||
|
||
describe('readJSON()', () => { | ||
it('reads package.json from the cwd', () => { | ||
const pkg = readJSON('package.json') | ||
expect(pkg instanceof Object).toBe(true) | ||
}) | ||
|
||
it('returns undefined for non-existent files', () => { | ||
expect(readJSON('x.json')).toBe(undefined) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,63 @@ | ||
const {dirname} = require('path') | ||
const now = require('./now') | ||
const getAlias = require('./get-alias') | ||
const getBranch = require('./get-branch') | ||
const commitStatus = require('./commit-status') | ||
const getBranchAlias = require('./get-alias') | ||
const now = require('./now') | ||
const readJSON = require('./read-json') | ||
|
||
module.exports = function deploy() { | ||
const nowJson = readJSON('now.json') || {} | ||
const packageJson = readJSON('package.json') || {} | ||
const rulesJson = readJSON('rules.json') | ||
|
||
module.exports = function deploy(args) { | ||
const nowJson = require('./now-json') | ||
const packageJson = require('./package-json') | ||
const name = nowJson.name || packageJson.name || dirname(process.cwd()) | ||
const branch = getBranch(name) | ||
|
||
console.log(`[deploy] deploying "${name}" with now...`) | ||
return now(args) | ||
return now() | ||
.then(url => { | ||
console.log(`[deploy] deployed to: ${url}`) | ||
return {name, root: url, url} | ||
if (url) { | ||
// console.log(`[deploy] deployed to: ${url}`) | ||
return { | ||
name, | ||
root: url, | ||
url | ||
} | ||
} else { | ||
throw new Error(`Unable to get deployment URL from now: ${url}`) | ||
} | ||
}) | ||
.then(res => { | ||
const {url} = res | ||
const alias = getAlias(name) | ||
if (alias) { | ||
res.url = alias | ||
return now(['alias', ...args, url, alias]) | ||
.then(() => commitStatus(alias)) | ||
const prodAlias = nowJson.alias | ||
if (branch === 'master' && !rulesJson) { | ||
res.url = prodAlias | ||
return now(['alias', url, prodAlias]) | ||
.then(() => commitStatus(prodAlias)) | ||
.then(() => res) | ||
} | ||
const branchAlias = getBranchAlias(name, branch) | ||
if (branchAlias) { | ||
res.url = res.alias = branchAlias | ||
return now(['alias', url, branchAlias]) | ||
.then(() => commitStatus(branchAlias)) | ||
.then(() => { | ||
if (branch === 'master' && rulesJson) { | ||
const {alias} = res | ||
if (!prodAlias) { | ||
console.warn(`[deploy] no alias field in now.json!`) | ||
return res | ||
} else if (prodAlias === alias) { | ||
console.warn(`[deploy] already aliased to production URL: ${alias}; skipping rules.json`) | ||
return res | ||
} | ||
res.url = prodAlias | ||
return now(['alias', alias, prodAlias, '-r', 'rules.json']).then(() => commitStatus(prodAlias)) | ||
} | ||
}) | ||
.then(() => res) | ||
} else { | ||
return res | ||
} | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const {resolve} = require('path') | ||
const {existsSync} = require('fs') | ||
|
||
module.exports = function readJSON(path) { | ||
const resolved = resolve(process.cwd(), path) | ||
return existsSync(resolved) ? require(resolved) : undefined | ||
} |