Skip to content

Commit

Permalink
haxtheweb/issues#2193 clean up from end of day
Browse files Browse the repository at this point in the history
  • Loading branch information
btopro committed Dec 12, 2024
1 parent 70a70a5 commit fc6ca47
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 32 deletions.
29 changes: 20 additions & 9 deletions src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,14 @@ async function main() {
.option('--title-scrape <char>', 'CSS Selector for `title` in resource')
.option('--content-scrape <char>', 'CSS Selector for `body` in resource')
.option('--items-import <char>', 'import items from a file / site')
.option('--recipe <char>', 'path to recipe file')
.version(await HAXCMS.getHAXCMSVersion())
.helpCommand(true);

// default command which runs interactively
program
.command('start')
.description('Interactive program to pick options')
.description('Select which hax sub-program to run')
.action(() => {
commandRun = {
command: 'start',
Expand All @@ -81,8 +82,8 @@ async function main() {
strActions+= `${action.value} - ${action.label}` + "\n\r";
});
let siteProg = program
.command('site');
siteProg
.command('site')
.description('create or administer a HAXsite')
.argument('[action]', 'Actions to perform on site include:' + "\n\r" + strActions)
.action((action) => {
commandRun = {
Expand All @@ -95,19 +96,29 @@ async function main() {
commandRun.options.skip = true;
}
})
.option('--path <char>', 'path the project should be created in')
.option('--v', 'Verbose output')
.option('--debug', 'Output for developers')
.option('--format <char>', 'Output format; json (default), yaml')
.option('--path <char>', 'where to perform operation')
.option('--npm-client <char>', 'npm client to use (must be installed) npm, yarn, pnpm', 'npm')
.option('--y', 'yes to all questions')
.option('--skip', 'skip frills like animations')
.option('--quiet', 'remove console logging')
.option('--auto', 'yes to all questions, alias of y')
.option('--no-i', 'prevent interactions / sub-process, good for scripting')
.option('--to-file <char>', 'redirect command output to a file')
.option('--no-extras', 'skip all extra / automatic command processing')
.option('--root <char>', 'root location to execute the command from')

.option('--import-site <char>', 'URL of site to import')
.option('--import-structure <char>', `import method to use:\n\rpressbooksToSite\n\relmslnToSite\n\rhaxcmsToSite\n\rnotionToSite\n\rgitbookToSite\n\revolutionToSite\n\rhtmlToSite\n\rdocxToSite`)
.option('--name <char>', 'name of the site (when creating a new one)')
.option('--domain <char>', 'published domain name')
.option('--node-op <char>', 'node operation to perform')
.option('--no-i', 'prevent interactions / sub-process, good for scripting')
.option('--title-scrape <char>', 'CSS Selector for `title` in resource')
.option('--content-scrape <char>', 'CSS Selector for `body` in resource')
.option('--to-file <char>', 'redirect command output to a file')
.option('--no-extras', 'skip all extra / automatic command processing')
.option('--item-import <char>', 'import items from a file / site')
.option('--root <char>', 'root location to execute the command from')
.option('--recipe <char>', 'path to recipe file')
.version(await HAXCMS.getHAXCMSVersion());
let siteNodeOps = siteNodeOperations();
for (var i in siteNodeOps) {
Expand Down Expand Up @@ -415,7 +426,7 @@ async function main() {
if (results.type === "site" && !commandRun.options.theme) {
// support having no theme but autoselecting
if (commandRun.options.auto && commandRun.options.skip) {
commandRun.options.theme = themes[0];
commandRun.options.theme = themes[0].value;
}
else {
return p.select({
Expand Down
4 changes: 2 additions & 2 deletions src/lib/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const logger = winston.createLogger({
});

export function haxCliEnvOptions() {
return ['skip','npmClient','i','extras','root','path','org','author'];
return ['skip','npmClient','i','extras','root','path','org','author', 'y', 'auto'];
}

// wrapper so we can silence all log messages at the same time
Expand All @@ -51,7 +51,7 @@ export function commandString(commandRun) {
comStr+= ` --no-${camelToDash(key)}`;
}
else {
comStr+= ` --${camelToDash(key)}="${commandRun.options[key]}"`;
comStr+= ` --${camelToDash(key)} "${commandRun.options[key]}"`;
}
}
}
Expand Down
64 changes: 43 additions & 21 deletions src/lib/programs/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ exec('surge --version', error => {
});

const siteRecipeFile = 'create-cli.recipe';
const siteLoggingName = 'cli';
const logLevels = {};
logLevels[siteLoggingName] = 0;

// fake response class so we can capture the response from the headless route as opposed to print to console
// this way we can handle as data or if use is requesting output format to change we can respond
Expand Down Expand Up @@ -92,10 +95,8 @@ export async function siteCommandDetected(commandRun) {
filename: recipeFileName
});
const recipe = winston.createLogger({
levels: {
hax: 0
},
level: 'hax',
levels: logLevels,
level: siteLoggingName,
transports: [
recipeLogTransport
],
Expand Down Expand Up @@ -327,7 +328,7 @@ export async function siteCommandDetected(commandRun) {
if (!commandRun.options.quiet) {
log(`${josImport.items.length} nodes imported`);
}
recipe.log('hax', commandString(commandRun));
recipe.log(siteLoggingName, commandString(commandRun));
}
else if (!commandRun.options.quiet) {
log('Must specify --item-import as path to valid item export file or URL', 'error');
Expand Down Expand Up @@ -509,7 +510,7 @@ export async function siteCommandDetected(commandRun) {
createNodeBody.node.contents = locationContent;
}
let resp = await haxcmsNodejsCli.cliBridge('createNode', createNodeBody);
recipe.log('hax', commandString(commandRun));
recipe.log(siteLoggingName, commandString(commandRun));
if (commandRun.options.v) {
log(resp.res.data, 'silly');
}
Expand Down Expand Up @@ -633,7 +634,7 @@ export async function siteCommandDetected(commandRun) {
}
// if we have content (meaning it's not blank) then try to write the page location
if (locationContent && await page.writeLocation(locationContent)) {
recipe.log('hax', commandString(commandRun));
recipe.log(siteLoggingName, commandString(commandRun));
if (!commandRun.options.quiet) {
log(`node:edit success updated page content: "${page.id}`);
}
Expand All @@ -654,7 +655,7 @@ export async function siteCommandDetected(commandRun) {
page[commandRun.options.nodeOp] = commandRun.options[commandRun.options.nodeOp];
}
let resp = await activeHaxsite.updateNode(page);
recipe.log('hax', commandString(commandRun));
recipe.log(siteLoggingName, commandString(commandRun));
if (commandRun.options.v) {
log(resp, 'silly');
}
Expand Down Expand Up @@ -693,7 +694,7 @@ export async function siteCommandDetected(commandRun) {
console.warn(`node:delete failed "${commandRun.options.itemId} not found`);
}
else {
recipe.log('hax', commandString(commandRun));
recipe.log(siteLoggingName, commandString(commandRun));
log(`"${commandRun.options.itemId}" deleted`);
}
}
Expand Down Expand Up @@ -732,7 +733,7 @@ export async function siteCommandDetected(commandRun) {
if (themes && commandRun.options.theme && themes[commandRun.options.theme]) {
activeHaxsite.manifest.metadata.theme = themes[commandRun.options.theme];
activeHaxsite.manifest.save(false);
recipe.log('hax', commandString(commandRun));
recipe.log(siteLoggingName, commandString(commandRun));
}
}
}
Expand Down Expand Up @@ -868,9 +869,8 @@ export async function siteCommandDetected(commandRun) {
case "recipe:read":
// just print the recipe out
if (fs.existsSync(path.join(process.cwd(), `${siteRecipeFile}`))) {
let recContents = await fs.readFileSync(path.join(process.cwd(), `${siteRecipeFile}`));
let recContents = await fs.readFileSync(path.join(process.cwd(), `${siteRecipeFile}`),'utf8');
console.log(recContents);
log(recContents);
}
break;
case "recipe:play":
Expand All @@ -881,13 +881,37 @@ export async function siteCommandDetected(commandRun) {
message: `Select recipe:`,
defaultValue: process.cwd(),
initialValue: process.cwd(),
validate: (val) => {
if (!val.endsWith('.recipe')) {
return 'HAX Recipe files must end in .recipe';
}
}
});
}
if (fs.existsSync(commandRun.options.recipe)) {
let recContents = await fs.readFileSync(commandRun.options.recipe);
let recContents = await fs.readFileSync(commandRun.options.recipe,'utf8');
// split into commands
let commandList = recContents.split('hax: ');
console.log(commandList);
let commandList = recContents.replaceAll('cli: ', '').split("\n");
// confirm each command or allow --y so that it auto applies
for (var i in commandList) {
// verify every command starts this way for safety
if (commandList[i].startsWith('hax site')) {
let confirmation;
if (commandRun.options.y) {
confirmation = true;
}
else {
confirmation = await p.confirm({
message: `Do you want to run ${commandList[i]}? (This cannot be undone)`,
initialValue: true,
});
}
// confirmed; let's run!
if (confirmation) {
await exec(`${commandList[i]} --y --no-i --auto --quiet`);
}
}
}
}
break;
case "quit":
Expand Down Expand Up @@ -1100,15 +1124,13 @@ export async function siteProcess(commandRun, project, port = '3000') { // au
await hax.RoutesMap.post.createSite({body: siteRequest}, res);
// path different for this one as it's on the fly produced
const recipeFileName = path.join(project.path, '/', project.name, `${siteRecipeFile}`);
console.log(recipeFileName);
const recipeLogTransport = new winston.transports.File({
filename: recipeFileName
});

const recipe = winston.createLogger({
levels: {
hax: 0
},
level: 'hax',
levels: logLevels,
level: siteLoggingName,
transports: [
recipeLogTransport
],
Expand All @@ -1117,7 +1139,7 @@ export async function siteProcess(commandRun, project, port = '3000') { // au
// matching the common object elsewhere tho different reference in this command since it creates from nothing
// capture this if use input on the fly
commandRun.options.theme = project.theme;
recipe.log('hax', commandString(commandRun));
recipe.log(siteLoggingName, commandString(commandRun));
if (commandRun.options.v) {
if (commandRun.options.format === 'yaml') {
log(dump(res.data), 'silly');
Expand Down

0 comments on commit fc6ca47

Please sign in to comment.