Skip to content

Commit

Permalink
fix: null in yml files (#10)
Browse files Browse the repository at this point in the history
* fix: null in yml files

This commit fixes null values in YML files by removing all
nulls/undefinds from the source object/array recursively.

Closes #8.


* feat: add Node 16

This commit adds Node 16 to Node Major Versions

Co-authored-by: Mestery <mestery@pm.me>
  • Loading branch information
Aakodal and Mesteery authored Apr 20, 2021
1 parent b69ad18 commit 2fdbd10
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 44 deletions.
90 changes: 46 additions & 44 deletions src/ci/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { packageJson, yaml } from 'mrm-core';
import { minVersion, satisfies } from 'semver';

import { format, isUsingYarn } from '../utils';
import { cleanObjectOrArray, format, isUsingYarn } from '../utils';

const NODE_MAJOR_VERSIONS = Array.from({ length: 15 }, (_, index) => (index + 1).toString());
const NODE_MAJOR_VERSIONS = Array.from({ length: 16 }, (_, index) => (index + 1).toString());

module.exports = function task() {
const pkg = packageJson();
Expand All @@ -22,56 +22,58 @@ module.exports = function task() {
const scriptRunCmd = usingYarn ? 'yarn' : 'npm run';

yaml('.github/workflows/main.yml')
.merge({
name: 'Continuous Integration',
on: {
push: {
branches: ['main'],
'paths-ignore': ['*.{md,txt}', 'LICENSE', '.*config', '.vscode'],
'tags-ignore': ['*'],
.merge(
cleanObjectOrArray({
name: 'Continuous Integration',
on: {
push: {
branches: ['main'],
'paths-ignore': ['*.{md,txt}', 'LICENSE', '.*config', '.vscode'],
'tags-ignore': ['*'],
},
pull_request: {
branches: ['*'],
'paths-ignore': ['*.{md,txt}', 'LICENSE', '.*config', '.vscode'],
},
},
pull_request: {
branches: ['*'],
'paths-ignore': ['*.{md,txt}', 'LICENSE', '.*config', '.vscode'],
},
},

jobs: {
main: {
'runs-on': 'ubuntu-20.04',
jobs: {
main: {
'runs-on': 'ubuntu-20.04',

strategy: singleVersion
? undefined
: {
matrix: {
'node-version': versions,
},
},
strategy: singleVersion
? undefined
: {
matrix: {
'node-version': versions,
},
},

steps: [
{ uses: 'actions/checkout@v2' },
{
name: `Use Node.js v${singleVersion ?? '${{ matrix.node-version }}'}`,
uses: 'actions/setup-node@v2',
with: {
'node-version': singleVersion ?? '${{ matrix.node-version }}',
steps: [
{ uses: 'actions/checkout@v2' },
{
name: `Use Node.js v${singleVersion ?? '${{ matrix.node-version }}'}`,
uses: 'actions/setup-node@v2',
with: {
'node-version': singleVersion ?? '${{ matrix.node-version }}',
},
},
},

{ name: 'Install dependencies', run: usingYarn ? 'yarn install --immutable' : 'npm ci' },
{ name: 'Install dependencies', run: usingYarn ? 'yarn install --immutable' : 'npm ci' },

pkg.getScript('build')
? {
name: 'Build',
run: `${scriptRunCmd} build ${usingYarn ? '' : '-- '}--noEmit`,
}
: undefined,
pkg.getScript('test') ? { name: 'Test', run: `${scriptRunCmd} lint` } : undefined,
pkg.getScript('lint') ? { name: 'Lint', run: `${scriptRunCmd} lint` } : undefined,
],
pkg.getScript('build')
? {
name: 'Build',
run: `${scriptRunCmd} build ${usingYarn ? '' : '-- '}--noEmit`,
}
: undefined,
pkg.getScript('test') ? { name: 'Test', run: `${scriptRunCmd} lint` } : undefined,
pkg.getScript('lint') ? { name: 'Lint', run: `${scriptRunCmd} lint` } : undefined,
],
},
},
},
})
}),
)
.save();
format(['.github/workflows/main.yml']);
};
Expand Down
23 changes: 23 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { spawnSync } from 'child_process';
import { Stats, statSync } from 'fs';
import { packageJson } from 'mrm-core';

type ArrayOrObject = unknown[] | Record<PropertyKey, unknown>;

const PackagePropertiesOrder = [
'name',
'description',
Expand All @@ -26,6 +28,27 @@ const PackagePropertiesOrder = [
'keywords',
];

function* cleanArray(array: unknown[]): Generator {
for (const value of array) {
// eslint-disable-next-line eqeqeq
if (value != undefined) {
yield typeof value === 'object' ? cleanObjectOrArray(value as ArrayOrObject) : value;
}
}
}

function* cleanObject(object: Record<PropertyKey, unknown>): Generator<unknown[]> {
for (const entry of cleanArray(Object.entries(object)) as Iterable<unknown[]>) {
if (entry.length === 2) {
yield entry;
}
}
}

export function cleanObjectOrArray(object: ArrayOrObject): ArrayOrObject {
return Array.isArray(object) ? [...cleanArray(object)] : Object.fromEntries(cleanObject(object));
}

export function execCommand(command: string, args: string[] = []): void {
spawnSync(command, args, {
stdio: 'inherit',
Expand Down

0 comments on commit 2fdbd10

Please sign in to comment.