Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure any trailing whitespace in package.json is preserved. #64

Merged
merged 1 commit into from May 16, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions bin/ember-source-channel-url
Expand Up @@ -7,6 +7,7 @@ const fs = require('fs');
const getChannelURL = require('../src');
const channel = process.argv[2];
const shouldUpdatePackage = process.argv.includes('-w') || process.argv.includes('--write');
const DETECT_TRAILING_WHITESPACE = /\s+$/;

function printUsage() {
console.log(`
Expand Down Expand Up @@ -55,6 +56,7 @@ if (['release', 'beta', 'canary'].indexOf(channel) === -1) {
}

let contents = fs.readFileSync('package.json', { encoding: 'utf8' });
let trailingWhitespace = DETECT_TRAILING_WHITESPACE.exec(contents);
let pkg = JSON.parse(contents);

let dependencyType = ['dependencies', 'devDependencies'].find(
Expand All @@ -65,6 +67,11 @@ if (['release', 'beta', 'canary'].indexOf(channel) === -1) {
pkg[dependencyType]['ember-source'] = url;

let updatedContents = JSON.stringify(pkg, null, 2);

if (trailingWhitespace) {
updatedContents += trailingWhitespace[0];
}

fs.writeFileSync('package.json', updatedContents, { encoding: 'utf8' });
} else {
console.log(
Expand Down
16 changes: 16 additions & 0 deletions tests/index-test.js
Expand Up @@ -120,6 +120,22 @@ QUnit.module('ember-source-channel-url', function(hooks) {
});
});

QUnit.test('preserves line ending when updating package.json', function(assert) {
fs.writeFileSync(
'package.json',
JSON.stringify({ dependencies: { 'ember-source': '^3.10.0' } }, null, 2) + '\n',
{ encoding: 'utf8' }
);

return execa(EXECUTABLE_PATH, ['canary', '--write']).then(results => {
assert.ok(results.stdout.includes(this.expectedURL), 'URL is present in stdout');

let expected =
JSON.stringify({ dependencies: { 'ember-source': this.expectedURL } }, null, 2) + '\n';
assert.deepEqual(fs.readFileSync('package.json', { encoding: 'utf8' }), expected);
});
});

QUnit.test('fails when package.json is missing', function(assert) {
return execa(EXECUTABLE_PATH, ['canary', '--write']).catch(results => {
assert.ok(results.stdout.includes(this.expectedURL), 'URL is present in stdout');
Expand Down