Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@mason-api/cli

Package Overview
Dependencies
Maintainers
3
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mason-api/cli - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

4

package.json
{
"name": "@mason-api/cli",
"version": "1.0.0",
"version": "1.0.1",
"description": "CLI assistant for Mason builder",

@@ -40,3 +40,3 @@ "main": "dist/index.js",

"@mason-api/generator": "^1.0.0",
"@mason-api/javascript-sdk": "^3.2.12",
"@mason-api/javascript-sdk": "^3.3.0",
"babel-polyfill": "^6.26.0",

@@ -43,0 +43,0 @@ "colors": "^1.3.3",

@@ -8,4 +8,3 @@ import { getAPIKey } from './login';

export const defaultCommand = process.argv.length !== 2 ? '$0 ' : '';
export const command = `${defaultCommand}<format> <project> [component] [pages..]`;
export const command = 'generate <format> <project> [component] [pages..]';
export const desc = 'Generate template out of a component page';

@@ -38,6 +37,2 @@ export const builder = (yargs) => {

})
.option('verbose', {
alias: 'v',
describe: 'Output additional information',
})
.option('output', {

@@ -66,3 +61,3 @@ alias: 'o',

try {
json = await fetchProject(apiKey, argv.project, verbose);
json = await fetchProject(apiKey, argv.project, verbose, argv.verbose);
} catch (e) {

@@ -99,3 +94,3 @@ return console.log(colors.red(`Could not fetch project ${colors.bold(argv.project)} for api key ${colors.bold(apiKey)}`));

}
datasource.data = await fetchDatasource(datasource);
datasource.data = await fetchDatasource(datasource, argv.verbose);
}

@@ -111,7 +106,17 @@ }));

}
const template = generate(component.config, page, {
datasources: project.datasources,
embedData: argv.format === 'html' ? true : argv.embedData,
format: argv.format,
});
let template;
try {
template = generate(component.config, page, {
datasources: project.datasources,
embedData: argv.format === 'html' ? true : argv.embedData,
format: argv.format,
});
} catch (e) {
console.log('Failed to generate file');
if (verbose) {
console.log(e);
} else {
console.log('Set -v option to see the details of exceptions');
}
}
if (argv.f || argv.o) {

@@ -118,0 +123,0 @@ const dir = _.join(_.compact([

import _ from 'lodash';
import colors from 'colors/safe';
import path from 'path';
import spawn from 'child_process';
import { formats, getDatasourceIds } from '@mason-api/generator';
import { getAPIKey, getIdToken } from './login';
import { getAPIKey, getIdToken, getUserId } from './login';
import { fetchProjects, prompt, promptChoice } from '../utils';
export const command = process.argv.length === 2 ? '$0' : 'guide';
export const command = 'guide';
export const desc = 'Guide through template generation';
export const handler = async () => {
export const handler = async (argv) => {
console.log(colors.bold('Welcome to Mason wizard!'));

@@ -16,2 +18,3 @@ console.log(`It will guide you through a process to generate static templates

const apiKey = await getAPIKey(false);
const userId = await getUserId(false);

@@ -32,4 +35,7 @@ console.log(colors.bold('\nWhich format would you like to use?'));

console.log(colors.bold('\nWhich project would you like to work with?'));
const projects = await fetchProjects(idToken);
const projectId = await promptChoice('Project: ', 'project id', _.mapValues(_.keyBy(projects, 'id'), project => project.name));
const projects = await fetchProjects(idToken, argv.verbose);
const projectId = await promptChoice('Project: ', 'project id', _.mapValues(
_.keyBy(_.filter(projects, p => !p.isArchived && _.findIndex(p.members, ['id', userId]) > -1), 'id'),
project => project.name,
));
const project = _.find(projects, ['id', projectId]);

@@ -65,3 +71,3 @@

console.log(colors.italic(colors.gray('Leave blank to place files in the current directory')));
const outputDir = _.trim(await prompt('Output:')) || '.';
const outputDir = _.trim(await prompt('Output: ')) || '.';
params.push(`-o ${outputDir}`);

@@ -135,7 +141,16 @@ // }

params.push(`-k ${apiKey}`);
console.log(colors.bold('\nExecuting command: '));
console.log(colors.bold('\nGenerated command: '));
console.log(colors.italic(colors.gray('Use this command next time to avoid going through wizard again\nFor example you can use this as a part of your build process')));
const args = _.replace(_.join(_.compact([format, projectId, componentId, page, ...params]), ' '), '*', '\\*');
console.log(colors.green(`mason ${args}`));
const argList = [format, projectId, componentId, page, ...params];
const args = _.join(_.compact(argList), ' ');
console.log(colors.green(`mason ${_.replace(args, '*', '\\*')}`));
console.log(colors.bold('\nExecute command now?'));
console.log(colors.gray(`It will output to ${colors.white(path.resolve(__dirname, outputDir))} directory.`));
if (!_.startsWith(_.trim(await prompt(`${colors.bold('y')}/n: `)), 'n')) {
const { spawn } = require('child_process');
yargs.parse(`generate ${args}`);
}
};

@@ -51,2 +51,9 @@ import fs from 'fs';

}
export async function getUserId() {
try {
return JSON.parse(fs.readFileSync(`${os.homedir()}/.masonrc`)).user.id;
} catch (e) {
return null;
}
}

@@ -53,0 +60,0 @@ export async function getAPIKey(dontLogIn) {

#!/usr/bin/env node
import Yargs from 'yargs';
import BabelPolyfill from "babel-polyfill";
import BabelPolyfill from 'babel-polyfill';

@@ -9,21 +9,41 @@ const yargs = Yargs // eslint-disable-line

.commandDir('commands')
.command('$0', 'default', () => {}, (argv) => {
setTimeout(() => {
// guide is a default command if `mason` is called without positional arguments
if (argv._.length === 0) {
yargs.exec(`guide ${process.argv.slice(2).join(' ')}`);
// when arguments are provided but no command was matched, assume `generate` command
} else {
yargs.exec(`generate ${process.argv.slice(2).join(' ')}`);
}
}, 10);
})
.option('verbose', {
alias: 'v',
describe: 'Output additional information',
})
.option('api-key', {
alias: 'k',
describe: 'Key for authentication, can be set via $MASON_API_KEY',
})
});
yargs.parse(process.argv.slice(2), (err, args, output) => {
if (output) {
// Remove types (e.g. [string], [boolean]) from the output
output = output.replace('Positionals:', 'Arguments:');
output = output.replace(/\[\w+\]\n/g, '\n');
output = output.replace(/(?:\n\s+)?\[default:\s*(.*?)\]\n/g, '\n');
output = output.replace(/\[choices:\s*([\s\S]+?)\]\n/g, (m, choices) => {
return ' (' + choices.replace(/"/g, '').replace(/\s+/g, ' ') + ')\n';
})
output = output.replace(/\n\s*--version.*?\n/, '\n');
global.yargs = yargs;
// Show the modified output
console.log(output);
}
})
yargs.exec = (input) => {
yargs.parse(input, (err, args, output) => {
if (output) {
// Remove types (e.g. [string], [boolean]) from the output
output = output.replace('Positionals:', 'Arguments:');
output = output.replace(/\[\w+\]\n/g, '\n');
output = output.replace(/(?:\n\s+)?\[default:\s*(.*?)\]\n/g, '\n');
output = output.replace(/\[choices:\s*([\s\S]+?)\]\n/g, (m, choices) => ` (${choices.replace(/"/g, '').replace(/\s+/g, ' ')})\n`);
output = output.replace(/\n\s*--version.*?\n/, '\n');
// Show the modified output
console.log(output);
}
});
};
yargs.exec(process.argv.slice(2));

@@ -81,4 +81,8 @@ import readline from 'readline';

export async function fetchProject(apiKey, projectId) {
const response = await fetch(`${MASON_API_HOST}/serve?apiKey=${apiKey}&pIds=${projectId}&true&libraryVersion=3.2.4`);
export async function fetchProject(apiKey, projectId, verbose) {
const url = `${MASON_API_HOST}/serve?apiKey=${encodeURIComponent(apiKey)}&pIds=${projectId}&true&libraryVersion=3.2.4`;
if (verbose) {
console.log(` ${colors.gray(url)}`);
}
const response = await fetch(url);
if (response.status !== 200) {

@@ -90,4 +94,9 @@ throw new Error('Cant find');

export async function fetchProjects(idToken) {
const response = await fetch(`${MASON_API_HOST}/projects?idToken=${idToken}`);
export async function fetchProjects(idToken, verbose) {
const url = `${MASON_API_HOST}/projects?idToken=${idToken}`;
if (verbose) {
console.log(` ${colors.gray(url)}`);
}
const response = await fetch(url);
if (response.status !== 200) {

@@ -99,4 +108,8 @@ throw new Error('Cant find');

export async function fetchDatasource(datasource) {
const response = await fetch(datasource.url);
export async function fetchDatasource(datasource, verbose) {
const url = `${MASON_API_HOST}/projects?idToken=${datasource.url}`;
if (verbose) {
console.log(` ${colors.gray(url)}`);
}
const response = await fetch(url);
if (response.status !== 200) {

@@ -103,0 +116,0 @@ throw new Error('Cant find');

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc