Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | import fs from 'fs-extra';
import path from 'path';
import arg from 'arg';
import { fileURLToPath } from 'url';
import ns from '@salesforce-ux/sds-namespaces';
import { packageDirectory } from 'package-directory';
import inquirer from 'inquirer';
import { createMappings, ejectComponents } from './main.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const root = path.resolve(__dirname, '../');
const repoRoot = await packageDirectory();
const nodeModules = path.resolve(repoRoot, 'node_modules');
const hooksDir = path.resolve(nodeModules, '@salesforce-ux/sds-styling-hooks/src/props');
const packageObj = fs.readJsonSync(path.resolve(root, 'package.json'));
function parseArgumentsIntoOptions(rawArgs) {
const args = arg(
{
'--bootstrap': Boolean,
'--ns': String,
'--categories': Object,
'--components': Boolean,
'--inject': Boolean,
'--srcDir': String,
'--outputDir': String,
'--eject': Boolean,
'--deprecated': Boolean,
'--theme': Object,
'--force': Boolean,
'--version': Boolean,
'--help': Boolean,
'--interactive': Boolean,
'--scripts': Object,
},
{
argv: rawArgs.slice(2),
},
);
return {
bootstrap: args._.includes('bootstrap') || false,
interactive: args._.includes('interactive') || false,
eject: args._.includes('eject') || false,
ns: args['--ns'] || false,
components: args['--components'] || false,
srcDir: args['--srcDir'] || false,
outputDir: args['--outputDir'] || 'dist',
force: args['--force'] || false,
version: args['--version'] || false,
scripts: args._[0] || false,
};
}
async function promptForMissingOptions(options) {
let answers;
const categories = await recursiveReadDir(hooksDir);
if (options.bootstrap && options.ns) {
return {
...options,
ns: options.ns,
categories,
components: true,
};
}
const interactivePrompt = {
type: 'list',
name: 'scripts',
message: 'How can we help you today?',
choices: [
// TODO: The commented out options are not yet implemented and will be future additions
// { name: 'Create a new design system', value: 'newDs' },
// { name: 'Apply a design system theme to my styling hooks', value: 'applyTheme' },
// { name: 'Generate a design system component', value: 'generateComponent' },
{ name: 'Remap global styling hooks to my namespace', value: 'remapGlobal' },
{ name: 'Remap component styling hooks to my namespace', value: 'remapComponent' },
// {
// name: 'Remap global and component styling hooks to my namespace',
// value: 'remapGlobalAndComponent',
// },
// { name: 'I need to support deprecated global styling hooks', value: 'deprecatedGlobal' },
// { name: 'I need to support deprecated component styling hooks', value: 'deprecatedComponent' },
// {
// name: 'I need to support deprecated global and component styling hooks',
// value: 'deprecatedGlobalAndComponent',
// },
// { name: 'I want to make my component immutable', value: 'immutable' },
],
async validate(answer) {
if (answer.length < 1) {
return 'You must choose at least one choice.';
}
return true;
},
};
if (options.interactive) {
const init = await inquirer.prompt(interactivePrompt);
if (init.scripts === 'remapGlobal' && !options.ns) {
answers = await remapGlobal();
}
if (init.scripts === 'remapComponent' && !options.ns) {
answers = await remapComponent();
}
}
async function namespaceQuestion() {
return {
type: 'list',
name: 'ns',
message: 'What design system are you building on?',
choices: await getNamespace(),
filter(val) {
const prefix = val.match(/\((.*?)\)/i);
return prefix[1];
},
};
}
async function remapGlobal() {
const questions = [];
questions.push(await namespaceQuestion());
questions.push({
type: 'checkbox',
name: 'categories',
message: 'Select what hooks you want',
choices: categories,
validate(answer) {
if (answer.length < 1) {
return 'You must choose at least one category.';
}
return true;
},
});
return await inquirer.prompt(questions);
}
async function remapComponent() {
const questions = [];
questions.push(await namespaceQuestion());
return Object.assign(await inquirer.prompt(questions), { components: true });
}
// console.log(answers);
return { ...options, ...answers };
}
/**
* Get namespaces from @salesforce-ux/sds-namespaces
*/
async function getNamespace() {
return ns
.filter((entry) => {
if (entry.description !== 'Salesforce Design System') return entry;
})
.map((entry) => `${entry.description} (${entry.name})`);
}
/**
* Get categories from source directory
*/
const recursiveReadDir = async (folder) => {
const files = [];
const content = await fs.readdir(path.resolve(nodeModules, folder));
files.push(...content);
for (const entry of content) {
const ext = entry.split('.').pop();
if (ext !== 'json') {
const recursiveContent = await recursiveReadDir(`${folder}/${entry}`);
files.push(...recursiveContent);
}
}
return files
.filter((reduce) => reduce !== 'hooks.json')
.reduce((arr, file) => {
file = file.replace('.json', '');
arr.push(file);
return arr;
}, []);
};
export async function cli() {
let options = parseArgumentsIntoOptions(process.argv);
options = await promptForMissingOptions(options);
switch (true) {
case options.version:
console.log(`v${packageObj.version}`);
break;
case options.eject && !options.ns && !options.srcDir:
console.log('Ejecting requires a namespace, --ns=<ns>, and a source directory, --srcDir=<path>');
break;
case options.eject && Boolean(options.ns) && Boolean(options.srcDir):
console.log('Ejecting...');
await ejectComponents(options);
break;
case options.bootstrap && Boolean(options.ns):
console.log('Generating styling hooks...');
await createMappings(options);
break;
case options.interactive:
await createMappings(options);
break;
default:
console.log(`
sds-cli <command>
Usage:
sds-cli interactive interactive walkthrough
sds-cli bootstrap --ns=<ns> transform all sds categories and components styling hooks to namespace
sds-cli eject --ns=<ns> --srcDir=<path> ejects sds components to namespace
All commands:
bootstrap, eject, dist, force, srcDir, outputDir, interactive, ns
Specify on the command line via: sds-cli <command> --key=value
`);
break;
}
}
|