prettier diff /src/sort-scripts.ts for brc-dd

Created Diff never expires
1 removal
Lines
Total
Removed
Words
Total
Removed
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
62 lines
1 addition
Lines
Total
Added
Words
Total
Added
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
62 lines
import sortObject from 'sort-object-keys';
import sortObject from 'sort-object-keys';
import orderBy from 'sort-order';
import orderBy from 'sort-order';
import { PackageJson } from './types';
import { PackageJson } from './types';


const checkPre = (arg: string, scripts: PackageJson['scripts']): boolean =>
const checkPre = (arg: string, scripts: PackageJson['scripts']): boolean =>
/^pre/.test(arg) && scripts!.hasOwnProperty(arg.substr(3));
/^pre/.test(arg) && scripts!.hasOwnProperty(arg.substr(3));


const checkPost = (arg: string, scripts: PackageJson['scripts']): boolean =>
const checkPost = (arg: string, scripts: PackageJson['scripts']): boolean =>
/^post/.test(arg) && scripts!.hasOwnProperty(arg.substr(4));
/^post/.test(arg) && scripts!.hasOwnProperty(arg.substr(4));


// Sort alphabetically by script name excluding pre/post prefixes
// Sort alphabetically by script name excluding pre/post prefixes
function scriptName(this: PackageJson['scripts'], ...args: [string, string]): 1 | 0 | -1 {
function scriptName(this: PackageJson['scripts'], ...args: [string, string]): 1 | 0 | -1 {
const [a, b] = args.map((arg) =>
const [a, b] = args.map((arg) =>
checkPre(arg, this) || checkPost(arg, this) ? arg.replace(/^(pre|post)/, '') : arg,
checkPre(arg, this) || checkPost(arg, this) ? arg.replace(/^(pre|post)/, '') : arg
);
);
return a === b ? 0 : a < b ? -1 : 1;
return a === b ? 0 : a < b ? -1 : 1;
}
}


// Sort by pre, script, post
// Sort by pre, script, post
function prePostHooks(this: PackageJson['scripts'], a: string, b: string): 1 | 0 | -1 {
function prePostHooks(this: PackageJson['scripts'], a: string, b: string): 1 | 0 | -1 {
if (checkPre(a, this) || checkPost(b, this)) return -1;
if (checkPre(a, this) || checkPost(b, this)) return -1;
else if (checkPost(a, this) || checkPre(b, this)) return 1;
else if (checkPost(a, this) || checkPre(b, this)) return 1;
return 0;
return 0;
}
}


export default function sortScripts(scripts: PackageJson['scripts'] = {}): {
export default function sortScripts(scripts: PackageJson['scripts'] = {}): {
scripts?: PackageJson['scripts'];
scripts?: PackageJson['scripts'];
} {
} {
const order = orderBy(scriptName.bind(scripts), prePostHooks.bind(scripts));
const order = orderBy(scriptName.bind(scripts), prePostHooks.bind(scripts));
const keys = Object.keys(scripts) as never[];
const keys = Object.keys(scripts) as never[];
return keys.length === 0 ? {} : { scripts: sortObject(scripts, keys.sort(order)) };
return keys.length === 0 ? {} : { scripts: sortObject(scripts, keys.sort(order)) };
}
}


// config file: ink-markdown/.prettierrc.js
// config file: ink-markdown/.prettierrc.js
// link: https://github.com/cameronhunter/ink-markdown/blob/f97fb5cf1534d64c86e2ed3d412abacdb45ded6c/.prettierrc.js
// link: https://github.com/cameronhunter/ink-markdown/blob/f97fb5cf1534d64c86e2ed3d412abacdb45ded6c/.prettierrc.js
// note:
// note:
// `overrides` key is not reflected in sidebar,
// `overrides` key is not reflected in sidebar,
// but its not needed for this file bc its not ``.md`.
// but its not needed for this file bc its not ``.md`.
//
//
// module.exports = {
// module.exports = {
// printWidth: 120,
// printWidth: 120,
// tabWidth: 2,
// tabWidth: 2,
// useTabs: false,
// useTabs: false,
// semi: true,
// semi: true,
// singleQuote: true,
// singleQuote: true,
// jsxSingleQuote: true,
// jsxSingleQuote: true,
// trailingComma: 'none',
// trailingComma: 'none',
// bracketSpacing: true,
// bracketSpacing: true,
// jsxBracketSameLine: false,
// jsxBracketSameLine: false,
// arrowParens: 'always',
// arrowParens: 'always',
// proseWrap: 'always',
// proseWrap: 'always',
// overrides: [
// overrides: [
// {
// {
// files: '*.md',
// files: '*.md',
// options: {
// options: {
// tabWidth: 2,
// tabWidth: 2,
// printWidth: 80
// printWidth: 80
// }
// }
// }
// }
// ]
// ]
// };
// };