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 787dbc7 commit 97fbe6e
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 169 deletions.
6 changes: 5 additions & 1 deletion .madrun.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import baseConfig from './dev/.files/madrun/config.mjs';
* Customizations.
* <custom:start> */

export default $obj.mc.merge({}, baseConfig, {});
export default async (madrun) => {
return $obj.mc.merge({}, await baseConfig(madrun), {
// 'project:[cmd]': '', // Always prefix project CMDs.
});
};

/* </custom:end> */
14 changes: 7 additions & 7 deletions dev/.files/bin/includes/utilities.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const { pkgFile, pkgName, pkgPrivate, pkgRepository, pkgBuildAppType } = (() =>
}
const pkg = JSON.parse(fs.readFileSync(pkgFile).toString());

if (typeof pkg !== 'object') {
if (!_.isPlainObject(pkg)) {
throw new Error('u: Unable to parse `./package.json`.');
}
const pkgName = _.get(pkg, 'name', '');
Expand Down Expand Up @@ -148,7 +148,7 @@ export default class u {
}
const pkg = JSON.parse(fs.readFileSync(pkgFile).toString());

if (typeof pkg !== 'object') {
if (!_.isPlainObject(pkg)) {
throw new Error('u.pkg: Unable to parse `./package.json`.');
}
return pkg; // JSON object data.
Expand Down Expand Up @@ -189,7 +189,7 @@ export default class u {
const path = propsOrPath; // String path.
deeps.set(pkg, path, value, true, delimiter);
//
} else if (typeof propsOrPath === 'object') {
} else if (_.isPlainObject(propsOrPath)) {
const props = propsOrPath; // Object props.
$obj.mc.patch(pkg, props); // Potentially declarative ops.
} else {
Expand All @@ -209,7 +209,7 @@ export default class u {
const updates = JSON.parse((await fsp.readFile(updatesFile)).toString());
const sortOrder = JSON.parse((await fsp.readFile(sortOrderFile)).toString());

if (typeof updates !== 'object') {
if (!_.isPlainObject(updates)) {
throw new Error('u.prettifyPkg: Unable to parse `' + updatesFile + '`.');
}
if (!Array.isArray(sortOrder)) {
Expand Down Expand Up @@ -340,7 +340,7 @@ export default class u {
const githubUsername = String(process.env.USER_GITHUB_USERNAME).replace(/^@/u, '').toLowerCase();

let c10nUser = c10nUsers[githubUsername] || {};
c10nUser = c10nUser && typeof c10nUser === 'object' ? c10nUser : {};
c10nUser = _.isPlainObject(c10nUser) ? c10nUser : {};
_.set(c10nUser, 'github.username', githubUsername);

return c10nUser;
Expand Down Expand Up @@ -1163,7 +1163,7 @@ export default class u {

static async _npmjsOrgUserCanAdmin(org) {
try {
return 'object' === typeof (await u._npmjsOrgUsers(org));
return _.isPlainObject(await u._npmjsOrgUsers(org));
} catch {
return false; // Only admins|owners can list org members.
}
Expand All @@ -1172,7 +1172,7 @@ export default class u {
static async _npmjsOrgUsers(org) {
const members = JSON.parse(String(await u.spawn('npm', ['org', 'ls', org, '--json'], { quiet: true })));

if (typeof members !== 'object') {
if (!_.isPlainObject(members)) {
throw new Error('u._npmjsOrgMembers: Failed to acquire list of NPM team members for `' + org + '`.');
}
return members; // Keyed by username; values one of: `developer`, `admin`, or `owner`.
Expand Down
6 changes: 3 additions & 3 deletions dev/.files/bin/updater/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default async ({ projDir }) => {
}
const pkg = JSON.parse(fs.readFileSync(pkgFile).toString());

if (typeof pkg !== 'object') {
if (!_.isPlainObject(pkg)) {
throw new Error('updater.getPkg: Unable to parse `./package.json`.');
}
return pkg;
Expand Down Expand Up @@ -198,13 +198,13 @@ export default async ({ projDir }) => {
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 (typeof json !== 'object') {
if (!_.isPlainObject(json)) {
throw new Error('updater: Unable to parse `' + relPath + '`.');
}
if (fs.existsSync(jsonUpdatesFile)) {
const jsonUpdates = JSON.parse((await fsp.readFile(jsonUpdatesFile)).toString());

if (typeof jsonUpdates !== 'object') {
if (!_.isPlainObject(jsonUpdates)) {
throw new Error('updater: Unable to parse `' + jsonUpdatesFile + '`.');
}
if ('./package.json' === relPath && (await isPkgRepo('clevercanyon/skeleton-dev-deps'))) {
Expand Down
20 changes: 11 additions & 9 deletions dev/.files/madrun/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ import events from './includes/events.mjs';
/**
* `$ madrun` commands.
*/
export default {
'envs': './dev/.files/bin/envs.mjs {{@}}',
'install': './dev/.files/bin/install.mjs {{@}}',
'update': './dev/.files/bin/update.mjs {{@}}',
export default async (/* {cmd, args, ctx} */) => {
return {
'envs': './dev/.files/bin/envs.mjs {{@}}',
'install': './dev/.files/bin/install.mjs {{@}}',
'update': './dev/.files/bin/update.mjs {{@}}',

'dev': async (args) => 'npx vite dev' + (args.mode ? '' : ' --mode=dev') + ' {{@}}',
'preview': async (args) => 'npx vite preview' + (args.mode ? '' : ' --mode=dev') + ' {{@}}',
'build': async (args) => 'npx vite build' + (args.mode ? '' : ' --mode=prod') + ' {{@}}',
'wrangler': 'CLOUDFLARE_API_TOKEN="${USER_CLOUDFLARE_TOKEN:-}" npx wrangler {{@}}',
'dev': async ({ args }) => 'npx vite dev' + (args.mode ? '' : ' --mode=dev') + ' {{@}}',
'preview': async ({ args }) => 'npx vite preview' + (args.mode ? '' : ' --mode=dev') + ' {{@}}',
'build': async ({ args }) => 'npx vite build' + (args.mode ? '' : ' --mode=prod') + ' {{@}}',
'wrangler': 'CLOUDFLARE_API_TOKEN="${USER_CLOUDFLARE_TOKEN:-}" npx wrangler {{@}}',

...events, // e.g., `on::madrun:default:new`.
...events, // e.g., `on::madrun:default:new`.
};
};
28 changes: 16 additions & 12 deletions dev/.files/madrun/includes/events.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import path from 'node:path';
import { dirname } from 'desm';
import fsp from 'node:fs/promises';

import { $url } from '@clevercanyon/utilities';
import u from '../../bin/includes/utilities.mjs';
import { $url, $brand } from '@clevercanyon/utilities';

const __dirname = dirname(import.meta.url);
const projDir = path.resolve(__dirname, '../../../..');
Expand Down Expand Up @@ -51,18 +51,22 @@ export default {
*/

const dirBasename = path.basename(projDir);

const parentDir = path.dirname(projDir);
const parentDirBasename = path.basename(parentDir);

const parentDirBrand = $brand.get(parentDirBasename);
const parentDirOwner = parentDirBrand?.slug || parentDirBasename;

/**
* Updates `./package.json` in new project directory.
*/

await u.updatePkg({
name: args.pkgName || '@' + parentDirBasename + '/' + dirBasename,
repository: 'https://github.com/' + $url.encode(parentDirBasename) + '/' + $url.encode(dirBasename),
homepage: 'https://github.com/' + $url.encode(parentDirBasename) + '/' + $url.encode(dirBasename) + '#readme',
bugs: 'https://github.com/' + $url.encode(parentDirBasename) + '/' + $url.encode(dirBasename) + '/issues',
name: args.pkgName || '@' + parentDirOwner + '/' + dirBasename,
repository: 'https://github.com/' + $url.encode(parentDirOwner) + '/' + $url.encode(dirBasename),
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. */ [
'private', //
Expand Down Expand Up @@ -98,7 +102,7 @@ export default {
if (fs.existsSync(readmeFile)) {
let readme = fs.readFileSync(readmeFile).toString(); // Markdown.

readme = readme.replace(/@clevercanyon\/[^/?#\s]+/gu, args.pkgName || '@' + parentDirBasename + '/' + dirBasename);
readme = readme.replace(/@clevercanyon\/[^/?#\s]+/gu, args.pkgName || '@' + parentDirOwner + '/' + dirBasename);
await fsp.writeFile(readmeFile, readme); // Updates `./README.md` file.
}

Expand All @@ -123,18 +127,18 @@ export default {
* Attempts to create a remote repository origin at GitHub; if at all possible.
*/

if ('clevercanyon' === parentDirBasename) {
if ('clevercanyon' === parentDirOwner) {
if (process.env.GH_TOKEN && 'owner' === (await u.gistGetC10NUser()).github?.role) {
await u.spawn('gh', ['repo', 'create', parentDirBasename + '/' + dirBasename, '--source=.', args.public ? '--public' : '--private'], { stdio: 'inherit' });
await u.spawn('gh', ['repo', 'create', parentDirOwner + '/' + dirBasename, '--source=.', args.public ? '--public' : '--private'], { stdio: 'inherit' });
} else {
const origin = 'https://github.com/' + $url.encode(parentDirBasename) + '/' + $url.encode(dirBasename) + '.git';
const origin = 'https://github.com/' + $url.encode(parentDirOwner) + '/' + $url.encode(dirBasename) + '.git';
await u.spawn('git', ['remote', 'add', 'origin', origin], { stdio: 'inherit' });
}
} else if (process.env.USER_GITHUB_USERNAME === parentDirBasename) {
} else if (process.env.USER_GITHUB_USERNAME === parentDirOwner) {
if (process.env.GH_TOKEN) {
await u.spawn('gh', ['repo', 'create', parentDirBasename + '/' + dirBasename, '--source=.', args.public ? '--public' : '--private'], { stdio: 'inherit' });
await u.spawn('gh', ['repo', 'create', parentDirOwner + '/' + dirBasename, '--source=.', args.public ? '--public' : '--private'], { stdio: 'inherit' });
} else {
const origin = 'https://github.com/' + $url.encode(parentDirBasename) + '/' + $url.encode(dirBasename) + '.git';
const origin = 'https://github.com/' + $url.encode(parentDirOwner) + '/' + $url.encode(dirBasename) + '.git';
await u.spawn('git', ['remote', 'add', 'origin', origin], { stdio: 'inherit' });
}
}
Expand Down
Loading

0 comments on commit 97fbe6e

Please sign in to comment.