Skip to content

Commit

Permalink
feat: add basic support for esm (#914)
Browse files Browse the repository at this point in the history
* try load config as cjs and esm files

* use new async function

* fix tests

* run tests in node, not in jsdom environment

* Apply suggestions from code review

Co-authored-by: Haroen Viaene <[email protected]>

* add interop default, for mjs config

* fix lint error

Co-authored-by: Haroen Viaene <[email protected]>
Co-authored-by: shipjs <[email protected]>
  • Loading branch information
3 people authored Aug 24, 2020
1 parent 06790d1 commit 42918d5
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 32 deletions.
2 changes: 1 addition & 1 deletion packages/shipjs-lib/babel.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
presets: ['@babel/preset-env'],
presets: [['@babel/preset-env', { targets: { node: 'current' } }]],
};
1 change: 1 addition & 0 deletions packages/shipjs-lib/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module.exports = {
transform: {
'^.+\\.[t|j]sx?$': 'babel-jest',
},
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/src/lib/config/__tests__/example'],
watchPathIgnorePatterns: ['<rootDir>/sandbox'],
watchPlugins: [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
versionUpdated() {},
};
21 changes: 13 additions & 8 deletions packages/shipjs-lib/src/lib/config/__tests__/loadConfig.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,26 @@ const baseConfig = expect.objectContaining({});
const d = (dirName) => path.resolve(__dirname, dirName);

describe('loadConfig', () => {
it('does not throw when there is no config', () => {
expect(loadConfig(d('non-existing-dir'))).toMatchObject(baseConfig);
it('does not throw when there is no config', async () => {
expect(await loadConfig(d('non-existing-dir'))).toMatchObject(baseConfig);
});

it('should load config', () => {
expect(loadConfig(d('example'))).toMatchObject(baseConfig);
it('should load config', async () => {
expect(await loadConfig(d('example'))).toMatchObject(baseConfig);
});

it('should get function properly', () => {
const config = loadConfig(d('example'));
it('should load config with cjs ext', async () => {
const config = await loadConfig(d('cjs'));
expect(typeof config.versionUpdated).toBe('function');
});

it('should merge defaultConfig and userConfig', () => {
const config = loadConfig(d('example'));
it('should get function properly', async () => {
const config = await loadConfig(d('example'));
expect(typeof config.versionUpdated).toBe('function');
});

it('should merge defaultConfig and userConfig', async () => {
const config = await loadConfig(d('example'));
expect(config.conventionalChangelogArgs).toBe(
defaultConfig.conventionalChangelogArgs
);
Expand Down
30 changes: 26 additions & 4 deletions packages/shipjs-lib/src/lib/config/loadConfig.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
import dotenv from 'dotenv';
import { resolve } from 'path';
import { existsSync } from 'fs';
import { promises as fs, constants } from 'fs';

import defaultConfig from './defaultConfig';
import mergeConfig from './mergeConfig';
dotenv.config();

export default function loadConfig(dir = '.', filename = 'ship.config.js') {
const fullPath = resolve(dir, filename);
const userConfig = existsSync(fullPath) ? require(fullPath) : {};
const exist = (path) =>
fs
.access(path, constants.F_OK)
.then(() => path)
.catch(() => false);

const pickDefault = (obj) =>
typeof obj.default === 'object' ? obj.default : obj;

export default async function loadConfig(
dir = '.',
filename = 'ship.config',
extensions = ['js', 'cjs', 'mjs']
) {
const fullPaths = extensions.map((ext) => resolve(dir, `${filename}.${ext}`));

const fullPath = (
await Promise.all(fullPaths.map((path) => exist(path)))
).find((path) => path);

const userConfig =
fullPath && (await exist(fullPath))
? await import(fullPath).then(pickDefault)
: {};

return mergeConfig(defaultConfig, userConfig);
}
8 changes: 4 additions & 4 deletions packages/shipjs-lib/src/lib/util/getAppName.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { readFileSync } from 'fs';
import { promises as fs } from 'fs';
import { resolve } from 'path';
import loadConfig from '../config/loadConfig';

export default function getAppName(dir = '.') {
const { appName } = loadConfig(dir);
export default async function getAppName(dir = '.') {
const { appName } = await loadConfig(dir);
if (appName) {
return appName;
}
const { name } = JSON.parse(readFileSync(resolve(dir, 'package.json')));
const { name } = JSON.parse(await fs.readFile(resolve(dir, 'package.json')));
return name;
}
8 changes: 0 additions & 8 deletions packages/shipjs/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,4 @@ module.exports = {
},
],
],
plugins: [
[
'@babel/plugin-transform-runtime',
{
regenerator: true,
},
],
],
};
1 change: 1 addition & 0 deletions packages/shipjs/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module.exports = {
transform: {
'^.+\\.[t|j]sx?$': 'babel-jest',
},
testEnvironment: 'node',
watchPlugins: [
'jest-watch-typeahead/filename',
'jest-watch-typeahead/testname',
Expand Down
4 changes: 2 additions & 2 deletions packages/shipjs/src/flow/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async function prepare({
}
printDeprecated({ firstRelease, releaseCount });
checkGitHubToken({ dryRun });
const config = loadConfig(dir);
const config = await loadConfig(dir);
const { currentVersion, baseBranch } = validate({ config, dir });
const { remote, forcePushBranches } = config;
pull({ remote, currentBranch: baseBranch, dir, dryRun });
Expand Down Expand Up @@ -120,7 +120,7 @@ async function prepare({
dir,
dryRun,
});
const appName = getAppName(dir);
const appName = await getAppName(dir);
await notifyPrepared({
config,
appName,
Expand Down
4 changes: 2 additions & 2 deletions packages/shipjs/src/flow/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async function release({ help = false, dir = '.', dryRun = false }) {
printDryRunBanner();
}
checkGitHubToken({ dryRun });
const config = loadConfig(dir);
const config = await loadConfig(dir);
const { remote } = config;
const { currentVersion: version } = validate({ config, dir });
const {
Expand All @@ -35,7 +35,7 @@ async function release({ help = false, dir = '.', dryRun = false }) {
latestCommitUrl,
repoURL,
releaseTag,
} = gatherRepoInfo({ remote, version, dir });
} = await gatherRepoInfo({ remote, version, dir });
const isYarn = detectYarn(dir);
runTest({ isYarn, config, dir, dryRun });
runBuild({ isYarn, config, version, dir, dryRun });
Expand Down
6 changes: 3 additions & 3 deletions packages/shipjs/src/step/release/gatherRepoInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import {
} from 'shipjs-lib';
import runStep from '../runStep';

export default ({ remote, version, dir }) =>
runStep({ title: 'Gathering repository information.' }, () => {
const appName = getAppName(dir);
export default async ({ remote, version, dir }) =>
await runStep({ title: 'Gathering repository information.' }, async () => {
const appName = await getAppName(dir);
const latestCommitHash = getLatestCommitHash(dir);
const latestCommitUrl = getCommitUrl(remote, latestCommitHash, dir);
const { url: repoURL } = getRepoInfo(remote, dir);
Expand Down

0 comments on commit 42918d5

Please sign in to comment.