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

feat: support rc file from npm package #252

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 13 additions & 2 deletions lib/src/_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const Path = require('path');
const { js_beautify: beautify } = require('js-beautify');
const requireFromUrl = require('require-from-url/sync');
require('require-yaml');
const { spawnSync } = require('child_process');

/**
* Sort an object by its keys
Expand Down Expand Up @@ -307,7 +308,17 @@ function cleanConfig(confirm, path = process.cwd()) {
function getRemoteUrl(path) {
const pkgPath = Path.join(path, 'package.json');
const pkgExist = fs.existsSync(pkgPath);
const { gren = '' } = pkgExist ? require(pkgPath) : {};
if (!pkgExist) return '';
let {gren} = require(pkgPath);
if (!gren) return '';
if (!gren.startsWith('http')) {
const { stdout } = spawnSync('node', ['get-package-latest-version.js', gren], {
cwd: __dirname
});
const [errMsg, version] = JSON.parse(stdout.toString());
if (errMsg) throw new Error(errMsg);
gren = `https://cdn.jsdelivr.net/npm/${gren}@${version}/index.js`;
}
return gren;
}

Expand All @@ -316,7 +327,7 @@ function getRemoteUrl(path) {
* @since 0.18.0
* @private
*
* @param {string} path Path where to look for config files
* @param {string} url where to find the config file
* @return {Object} The configuration from the first found file or empty object
*/
function getConfigFromRemote(url) {
Expand Down
19 changes: 19 additions & 0 deletions lib/src/get-package-latest-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const https = require('https');

const packageName = process.argv[2];
const url = `https://registry.npmjs.org/-/package/${packageName}/dist-tags`;
https.get(url, res => {
const buffers = [];
res.on('data', (data) => {
buffers.push(data);
}).on('end', () => {
const result = [];
if (res.statusCode !== 200) {
result.push(`url: ${url}; statusCode: ${res.statusCode}`);
} else {
const { latest } = JSON.parse(Buffer.concat(buffers).toString());
result.push(null, latest);
}
process.stdout.write(JSON.stringify(result));
});
});
10 changes: 4 additions & 6 deletions test/_utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,15 @@ describe('_utils.js', () => {
describe('getRemoteUrl', () => {
const filename = process.cwd() + '/test/.temp/package.json';
const fileContent = {
gren: 'testUri'
gren: '@femessage/grenrc'
};

beforeEach(() => {
fs.writeFileSync(filename, JSON.stringify(fileContent));
});

it('Should always return a String', () => {
it('Should return a String', () => {
assert.isOk(typeof utils.getRemoteUrl(process.cwd() + '/test/.temp') === 'string', 'The type is a string');
assert.deepEqual(utils.getRemoteUrl(process.cwd() + '/test/.temp'), fileContent.gren, 'Given the right package path');
assert.deepEqual(utils.getRemoteUrl(process.cwd() + '/test'), '', 'Given a path with no package.json');
});

Expand All @@ -196,11 +195,10 @@ describe('_utils.js', () => {
});

describe('getConfigFromRemote', () => {
const grenRemote = 'https://raw.githubusercontent.com/FEMessage/github-release-notes/master/.grenrc.js';
const grenrc = require(process.cwd() + '/.grenrc.js');
const grenRemote = 'https://cdn.jsdelivr.net/npm/@femessage/[email protected]/index.js';

it('Should fetch config from remote url', () => {
assert.deepEqual(utils.getConfigFromRemote(grenRemote), grenrc, 'Given a remote gren config');
assert.isOk(typeof utils.getConfigFromRemote(grenRemote) === 'object', 'Given a remote gren config');
});
});

Expand Down