Skip to content

Commit

Permalink
Project update. [p][robotic]
Browse files Browse the repository at this point in the history
  • Loading branch information
jaswrks committed Feb 2, 2023
1 parent d91bec8 commit f40a62f
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 290 deletions.
59 changes: 10 additions & 49 deletions dev/.files/bin/includes/utilities.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
/* eslint-env es2021, node */

import _ from 'lodash';
import deeps from 'deeps';

import os from 'node:os';
import fs from 'node:fs';
Expand Down Expand Up @@ -61,27 +60,6 @@ const npmjsConfigVersion = '1.0.2'; // Bump when config changes in routines belo

const c10nLogo = path.resolve(__dirname, '../../assets/brands/c10n/logo.png');

$obj.mc.addOperation('$default', (current, defaults) => {
const paths = Object.keys(defaults);

for (const path of paths) {
if (undefined === deeps.get(current, path, '.')) {
deeps.set(current, path, defaults[path], true, '.');
}
}
return paths.length > 0;
});
$obj.mc.addOperation('$ꓺdefault', (current, defaults) => {
const paths = Object.keys(defaults);

for (const path of paths) {
if (undefined === deeps.get(current, path, 'ꓺ')) {
deeps.set(current, path, defaults[path], true, 'ꓺ');
}
}
return paths.length > 0;
});

/**
* Utilities.
*/
Expand Down Expand Up @@ -182,53 +160,36 @@ export default class u {
}
}

static async updatePkg(propsOrPath, value = undefined, delimiter = '.') {
static async updatePkg(propsOrPath = {}, value = undefined, delimiter = '.') {
const pkg = await u.pkg(); // Parses current `./package.json` file.

if (typeof propsOrPath === 'string') {
const path = propsOrPath; // String path.
deeps.set(pkg, path, value, true, delimiter);
$obj.mc.u.set(pkg, path, value, false, delimiter);
//
} else if (_.isPlainObject(propsOrPath)) {
const props = propsOrPath; // Object props.
$obj.mc.patch(pkg, props); // Potentially declarative ops.
} else {
throw new Error('u.updatePkg: Invalid arguments.');
}
await fsp.writeFile(pkgFile, JSON.stringify(pkg, null, 4));
await u.prettifyPkg(); // Sorts and runs prettier.
}

static async prettifyPkg() {
const pkg = {}; // Sorted `./package.json`; i.e., using insertion order.
const curPkg = await u.pkg(); // Parses current `./package.json` file.

const updatesFile = path.resolve(projDir, './dev/.files/bin/updater/data/package.json/updates.json');
const sortOrderFile = path.resolve(projDir, './dev/.files/bin/updater/data/package.json/sort-order.json');

const updates = JSON.parse((await fsp.readFile(updatesFile)).toString());
const sortOrder = JSON.parse((await fsp.readFile(sortOrderFile)).toString());

if (!_.isPlainObject(updates)) {
throw new Error('u.prettifyPkg: Unable to parse `' + updatesFile + '`.');
}
if (!Array.isArray(sortOrder)) {
throw new Error('u.prettifyPkg: Unable to parse `' + sortOrderFile + '`.');
throw new Error('u.updatePkg: Unable to parse `' + updatesFile + '`.');
}
if (await u.isPkgRepo('clevercanyon/skeleton-dev-deps')) {
if (updates.$ꓺdefault?.['devDependenciesꓺ@clevercanyon/skeleton-dev-deps']) {
delete updates.$ꓺdefault['devDependenciesꓺ@clevercanyon/skeleton-dev-deps'];
}
if (Array.isArray(updates.$ꓺunset)) {
updates.$ꓺunset.push('devDependenciesꓺ@clevercanyon/skeleton-dev-deps');
} else {
updates.$ꓺunset = ['devDependenciesꓺ@clevercanyon/skeleton-dev-deps'];
}
}
$obj.mc.patch(curPkg, updates); // Potentially declarative ops.

for (const path of sortOrder) {
const value = deeps.get(curPkg, path, 'ꓺ');
if (undefined !== value) deeps.set(pkg, path, value, true, 'ꓺ');
}
for (const [path, value] of Object.entries(deeps.flatten(curPkg, 'ꓺ'))) {
if (undefined === deeps.get(pkg, path, 'ꓺ')) deeps.set(pkg, path, value, true, 'ꓺ');
}
$obj.mc.patch(pkg, updates); // Potentially declarative ops.
const pkgPrettierCfg = { ...(await prettier.resolveConfig(pkgFile)), parser: 'json' };
await fsp.writeFile(pkgFile, prettier.format(JSON.stringify(pkg, null, 4), pkgPrettierCfg));
}
Expand Down Expand Up @@ -1092,7 +1053,7 @@ export default class u {

static async npmUpdate() {
await u.spawn('npm', ['update', '--save'], { stdio: 'inherit' });
await u.prettifyPkg(); // To our standards.
await u.updatePkg(); // To our standards.
}

static async npmPublish(opts = { dryRun: false }) {
Expand Down
57 changes: 0 additions & 57 deletions dev/.files/bin/updater/data/package.json/sort-order.json

This file was deleted.

62 changes: 60 additions & 2 deletions dev/.files/bin/updater/data/package.json/updates.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"configꓺc10nꓺ&ꓺgithubꓺenvsVersion": "",
"configꓺc10nꓺ&ꓺnpmjsꓺconfigVersions": ""
},
"$set": {
"$ꓺset": {
"funding": "https://github.com/sponsors/clevercanyon",
"workspaces": [],

Expand All @@ -70,5 +70,63 @@
"npm": "^8.19.3 || ^9.2.0"
}
},
"$unset": ["typings", "scripts"]
"$ꓺunset": ["typings", "scripts"],

"$ꓺpropSortOrder": [
"private",
"publishConfigꓺaccess",

"version",
"license",
"name",
"description",
"repository",
"homepage",
"bugs",
"funding",
"keywords",

"author",
"contributors",

"type",
"files",
"bin",
"imports",
"exports",
"sideEffects",
"module",
"main",
"browser",
"unpkg",
"types",
"typesVersions",

"dependencies",
"peerDependencies",
"peerDependenciesMeta",
"optionalDependencies",
"bundleDependencies",
"devDependencies",

"overrides",
"workspaces",

"cpu",
"os",
"enginesꓺnode",
"enginesꓺnpm",

"configꓺc10nꓺ&ꓺdotfilesꓺlock",

"configꓺc10nꓺ&ꓺbuildꓺappType",
"configꓺc10nꓺ&ꓺbuildꓺtargetEnv",

"configꓺc10nꓺ&ꓺgithubꓺteams",
"configꓺc10nꓺ&ꓺgithubꓺlabels",
"configꓺc10nꓺ&ꓺgithubꓺconfigVersion",
"configꓺc10nꓺ&ꓺgithubꓺenvsVersion",

"configꓺc10nꓺ&ꓺnpmjsꓺconfigVersions"
]
}
46 changes: 5 additions & 41 deletions dev/.files/bin/updater/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { dirname } from 'desm';
import fsp from 'node:fs/promises';

import chalk from 'chalk';
import deeps from 'deeps';
import prettier from 'prettier';

import { $str, $obj } from '@clevercanyon/utilities';
Expand All @@ -24,27 +23,6 @@ import { $cmd } from '@clevercanyon/utilities.node';

const { log } = console; // Shorter reference.

$obj.mc.addOperation('$default', (current, defaults) => {
const paths = Object.keys(defaults);

for (const path of paths) {
if (undefined === deeps.get(current, path, '.')) {
deeps.set(current, path, defaults[path], true, '.');
}
}
return paths.length > 0;
});
$obj.mc.addOperation('$ꓺdefault', (current, defaults) => {
const paths = Object.keys(defaults);

for (const path of paths) {
if (undefined === deeps.get(current, path, 'ꓺ')) {
deeps.set(current, path, defaults[path], true, 'ꓺ');
}
}
return paths.length > 0;
});

export default async ({ projDir }) => {
/**
* Initializes vars.
Expand Down Expand Up @@ -196,7 +174,6 @@ export default async ({ projDir }) => {
}
let json = JSON.parse((await fsp.readFile(path.resolve(projDir, relPath))).toString());
const jsonUpdatesFile = path.resolve(skeletonDir, './dev/.files/bin/updater/data', relPath, './updates.json');
const jsonSortOrderFile = path.resolve(skeletonDir, './dev/.files/bin/updater/data', relPath, './sort-order.json');

if (!_.isPlainObject(json)) {
throw new Error('updater: Unable to parse `' + relPath + '`.');
Expand All @@ -211,29 +188,16 @@ export default async ({ projDir }) => {
if (jsonUpdates.$ꓺdefault?.['devDependenciesꓺ@clevercanyon/skeleton-dev-deps']) {
delete jsonUpdates.$ꓺdefault['devDependenciesꓺ@clevercanyon/skeleton-dev-deps'];
}
if (Array.isArray(jsonUpdates.$ꓺunset)) {
jsonUpdates.$ꓺunset.push('devDependenciesꓺ@clevercanyon/skeleton-dev-deps');
} else {
jsonUpdates.$ꓺunset = ['devDependenciesꓺ@clevercanyon/skeleton-dev-deps'];
}
}
$obj.mc.patch(json, jsonUpdates); // Potentially declarative ops.
const prettierCfg = { ...(await prettier.resolveConfig(path.resolve(projDir, relPath))), parser: 'json' };
await fsp.writeFile(path.resolve(projDir, relPath), prettier.format(JSON.stringify(json, null, 4), prettierCfg));
}
if (fs.existsSync(jsonSortOrderFile)) {
const origJSON = _.cloneDeep(json); // Deep clone.
json = {}; // Sorted JSON file; i.e., using insertion order.
const jsonSortOrder = JSON.parse((await fsp.readFile(jsonSortOrderFile)).toString());

if (!Array.isArray(jsonSortOrder)) {
throw new Error('updater: Unable to parse `' + jsonSortOrderFile + '`.');
}
for (const path of jsonSortOrder) {
const value = deeps.get(origJSON, path, 'ꓺ');
if (undefined !== value) deeps.set(json, path, value, true, 'ꓺ');
}
for (const [path, value] of Object.entries(deeps.flatten(origJSON, 'ꓺ'))) {
if (undefined === deeps.get(json, path, 'ꓺ')) deeps.set(json, path, value, true, 'ꓺ');
}
const prettierCfg = { ...(await prettier.resolveConfig(path.resolve(projDir, relPath))), parser: 'json' };
await fsp.writeFile(path.resolve(projDir, relPath), prettier.format(JSON.stringify(json, null, 4), prettierCfg));
}
}

/**
Expand Down
20 changes: 10 additions & 10 deletions dev/.files/madrun/includes/events.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ export default {
homepage: 'https://github.com/' + $url.encode(parentDirOwner) + '/' + $url.encode(dirBasename) + '#readme',
bugs: 'https://github.com/' + $url.encode(parentDirOwner) + '/' + $url.encode(dirBasename) + '/issues',

$unset: /* Effectively resets these to default values. */ [
$ꓺunset: /* Effectively resets these to default values. */ [
'private', //
'publishConfig.access',
'publishConfigꓺaccess',

'version',
'license',
Expand All @@ -81,16 +81,16 @@ export default {
'author',
'contributors',

'config.c10n.&.github.teams',
'config.c10n.&.github.labels',
'config.c10n.&.github.configVersion',
'config.c10n.&.github.envsVersion',
'configꓺc10nꓺ&ꓺgithubꓺteams',
'configꓺc10nꓺ&ꓺgithubꓺlabels',
'configꓺc10nꓺ&ꓺgithubꓺconfigVersion',
'configꓺc10nꓺ&ꓺgithubꓺenvsVersion',

'config.c10n.&.npmjs.teams',
'config.c10n.&.npmjs.configVersions',
'configꓺc10nꓺ&ꓺnpmjsꓺteams',
'configꓺc10nꓺ&ꓺnpmjsꓺconfigVersions',
],
...(args.pkg ? { $set: { private: false } } : {}),
...(args.pkg && args.public ? { $set: { 'publishConfig.access': 'public' } } : {}),
...(args.pkg ? { $ꓺset: { private: false } } : {}),
...(args.pkg && args.public ? { $ꓺset: { publishConfigꓺaccess: 'public' } } : {}),
});

/**
Expand Down
Loading

0 comments on commit f40a62f

Please sign in to comment.