Skip to content

Commit

Permalink
Merge branch 'feature/es-modules'
Browse files Browse the repository at this point in the history
# Conflicts:
#	.github/workflows/test.yml
  • Loading branch information
webpro committed Apr 30, 2022
2 parents c8164b1 + 3c9ada7 commit 6547033
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 85 deletions.
16 changes: 14 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,27 @@ jobs:
- windows-latest
- macos-latest
node:
- 12
- 14
- 16

runs-on: ${{ matrix.os }}
name: ${{ matrix.os }} (Node v${{ matrix.node }})

steps:
- uses: actions/checkout@v2.3.4
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}

- name: Get npm cache directory
id: npm-cache
run: echo "::set-output name=dir::$(npm config get cache)"

- uses: actions/cache@v2.1.7
with:
path: ${{ steps.npm-cache.outputs.dir }}
key: ubuntu-latest-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: ubuntu-latest-node-

- name: Install dependencies
run: npm install
Expand Down
81 changes: 51 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
const fs = require('fs');
const util = require('util');
const { EOL } = require('os');
const glob = require('fast-glob');
const get = require('lodash.get');
const set = require('lodash.set');
const castArray = require('lodash.castarray');
const detectIndent = require('detect-indent');
const yaml = require('js-yaml');
const toml = require('@iarna/toml');
const ini = require('ini');
const semver = require('semver');
const { Plugin } = require('release-it');

const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
import { readFileSync, writeFileSync } from 'fs';
import { EOL } from 'os';
import glob from 'fast-glob';
import get from 'lodash.get';
import set from 'lodash.set';
import castArray from 'lodash.castarray';
import detectIndent from 'detect-indent';
import yaml from 'js-yaml';
import toml from '@iarna/toml';
import ini from 'ini';
import semver from 'semver';
import { Plugin } from 'release-it';

const noop = Promise.resolve();
const isString = a => typeof a === 'string';

const parseFileOption = option => {
const file = typeof option === 'string' ? option : option.file;
const file = isString(option) ? option : option.file;
const mimeType = typeof option !== 'string' ? option.type : null;
const path = (typeof option !== 'string' && option.path) || 'version';
return { file, mimeType, path };
Expand Down Expand Up @@ -54,9 +52,16 @@ class Bumper extends Plugin {
const { file, mimeType, path } = parseFileOption(option);
if (file) {
const type = getFileType(file, mimeType);
const data = await readFile(file, 'utf8').catch(() => '{}');
let data;

try {
data = readFileSync(file, 'utf8');
} catch (error) {
data = '{}';
}

const parsed = await parse(data, type);
const version = typeof parsed === 'string' ? parsed.trim() : get(parsed, path);
const version = isString(parsed) ? parsed.trim() : get(parsed, path);
return semver.parse(version).toString();
}
return null;
Expand All @@ -68,13 +73,21 @@ class Bumper extends Plugin {
const { latestVersion } = this.config.getContext();
if (!out) return;

const expandedOptions = castArray(out).map(options => (typeof options === 'string' ? { file: options } : options));
const expandedOptions = castArray(out).map(options => (isString(options) ? { file: options } : options));

const options = [];
for (const option of expandedOptions) {
if (glob.isDynamicPattern(option.file)) {
const files = await glob(option.file, { onlyFiles: true, unique: true });
options.push(...files.map(file => Object.assign({}, option, { file })));
const files = await glob(option.file, {
onlyFiles: true,
unique: true
});
options.push(
...files.map(file => ({
...option,
file
}))
);
} else options.push(option);
}

Expand All @@ -85,31 +98,39 @@ class Bumper extends Plugin {
if (isDryRun) return noop;

const type = getFileType(file, mimeType);
const data = await readFile(file, 'utf8').catch(() => (type === 'text' ? latestVersion : '{}'));

let data;

try {
data = readFileSync(file, 'utf8');
} catch (error) {
data = type === 'text' ? latestVersion : '{}';
}

const parsed = await parse(data, type);
const indent = typeof data === 'string' ? detectIndent(data).indent || ' ' : null;
const indent = isString(data) ? detectIndent(data).indent || ' ' : null;

if (typeof parsed !== 'string') {
castArray(path).forEach(path => set(parsed, path, version));
}

switch (type) {
case 'json':
return writeFile(file, JSON.stringify(parsed, null, indent) + '\n');
return writeFileSync(file, JSON.stringify(parsed, null, indent) + '\n');
case 'yaml':
return writeFile(file, yaml.dump(parsed, { indent: indent.length }));
return writeFileSync(file, yaml.dump(parsed, { indent: indent.length }));
case 'toml':
return writeFile(file, toml.stringify(parsed));
return writeFileSync(file, toml.stringify(parsed));
case 'ini':
return writeFile(file, ini.encode(parsed));
return writeFileSync(file, ini.encode(parsed));
default:
const versionMatch = new RegExp(latestVersion || '', 'g');
const write = parsed ? parsed.replace(versionMatch, version) : version + EOL;
return writeFile(file, write);
return writeFileSync(file, write);
}
})
);
}
}

module.exports = Bumper;
export default Bumper;
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "@release-it/bumper",
"version": "3.0.1",
"version": "4.0.0-esm.1",
"description": "Version read/write plugin for release-it",
"main": "index.js",
"type": "module",
"exports": "./index.js",
"scripts": {
"test": "bron test.js",
"release": "release-it"
Expand All @@ -29,26 +30,26 @@
},
"dependencies": {
"@iarna/toml": "^2.2.5",
"detect-indent": "^6.1.0",
"detect-indent": "^7.0.0",
"fast-glob": "^3.2.7",
"ini": "^2.0.0",
"ini": "^3.0.0",
"js-yaml": "^4.1.0",
"lodash.castarray": "^4.4.0",
"lodash.get": "^4.4.2",
"lodash.set": "^4.3.2",
"semver": "^7.3.5"
"semver": "^7.3.7"
},
"devDependencies": {
"bron": "^1.1.1",
"bron": "^2.0.3",
"mock-fs": "^5.1.2",
"release-it": "^14.10.0",
"sinon": "^12.0.1"
"release-it": "^15.0.0-esm.4",
"sinon": "^13.0.2"
},
"peerDependencies": {
"release-it": "^14.0.0"
"release-it": "^15.0.0-esm.4"
},
"engines": {
"node": ">=10"
"node": ">=14"
},
"release-it": {
"hooks": {
Expand Down
Loading

0 comments on commit 6547033

Please sign in to comment.