Skip to content

Commit

Permalink
Add eslint config. Lint.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmerrique committed Dec 29, 2021
1 parent 77b1487 commit 1e303f5
Show file tree
Hide file tree
Showing 24 changed files with 1,058 additions and 1,134 deletions.
21 changes: 21 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = {
root: true,
env: {
browser: false,
es2021: true,
node: true,
},
extends: [
"eslint:recommended",
"plugin:unicorn/recommended",
"plugin:prettier/recommended",
],
plugins: ["prettier"],
parserOptions: {
ecmaVersion: 13,
sourceType: "module",
},
rules: {
"unicorn/no-process-exit": "off",
},
};
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ test/data/*
npm-debug.log
.vscode
jsconfig.json
.history/
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

lint-staged
npx lint-staged
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"quoteProps": "consistent"
}
44 changes: 23 additions & 21 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import 'dotenv/config';
import yargs from 'yargs';
import {hideBin} from 'yargs/helpers';
import { hideBin } from 'yargs/helpers';
import debug from 'debug';
import path from 'node:path';
import fs from 'node:fs';
import https from 'node:https';
import codeVersion from './lib/branch.js';
import {commands} from './commands/index.js';
import { commands } from './commands/index.js';
debug('cli');

const jsonPath = path.join(process.cwd(), 'dw-cli.json');
Expand Down Expand Up @@ -104,42 +104,42 @@ yargs(hideBin(process.argv))
describe: 'Number of lines to print on each tail',
default: 100,
},
include: {
'include': {
alias: 'options.include',
describe: 'Log levels to include',
type: 'array',
default: [],
},
exclude: {
'exclude': {
alias: 'options.exclude',
describe: 'Log levels to exclude',
type: 'array',
default: [],
},
list: {
'list': {
alias: 'options.list',
describe: 'Output a list of log types found on the remote filesystem',
type: 'boolean',
default: false,
},
filter: {
'filter': {
alias: 'options.filter',
describe: 'Filter log messages by regexp',
default: undefined,
},
length: {
'length': {
alias: 'options.length',
describe: 'Length to truncate a log message',
default: undefined,
},
search: {
'search': {
alias: 'options.search',
describe:
'Instead of a tail, this will execute a search on all log files (useful for Production)',
type: 'boolean',
default: false,
},
timestamp: {
'timestamp': {
alias: 'options.timestamp',
describe:
'Convert the timestamp in each log message to your local computer timezone, use --no-timestamp to disable',
Expand Down Expand Up @@ -172,15 +172,15 @@ yargs(hideBin(process.argv))
)
.example('$0 clean dev01', 'Remove all inactive code versions on dev01')
.example('$0 log dev01', 'Stream log files from the dev01')
.option('username', {describe: 'Username for instance'})
.option('password', {alias: 'p', describe: 'Password for instance'})
.option('hostname', {alias: 'h', describe: 'Hostname for instance'})
.option('cartridges', {alias: 'c', describe: 'Path to cartridges'})
.option('api-version', {describe: 'Demandware API Version'})
.option('code-version', {alias: 'v', default: codeVersion()})
.option('client-id', {describe: 'Demandware API Client ID'})
.option('client-password', {describe: 'Demandware API Client Password'})
.config({extends: fs.existsSync(jsonPath) ? jsonPath : {}})
.option('username', { describe: 'Username for instance' })
.option('password', { alias: 'p', describe: 'Password for instance' })
.option('hostname', { alias: 'h', describe: 'Hostname for instance' })
.option('cartridges', { alias: 'c', describe: 'Path to cartridges' })
.option('api-version', { describe: 'Demandware API Version' })
.option('code-version', { alias: 'v', default: codeVersion() })
.option('client-id', { describe: 'Demandware API Client ID' })
.option('client-password', { describe: 'Demandware API Client Password' })
.config({ extends: fs.existsSync(jsonPath) ? jsonPath : {} })
.demandCommand(1)
.help()
.version().argv;
Expand All @@ -191,9 +191,11 @@ yargs(hideBin(process.argv))
* @type {import('yargs').MiddlewareFunction}
*/
function configure(argv) {
const instance = argv.instances
? argv.instances[String(process.argv.slice(3)[0])]
: {};
const instance =
argv.instances &&
typeof argv.instances[String(argv.instance)] !== 'undefined'
? argv.instances[String(argv.instance)]
: {};

// Required for API commands (versions, job)
argv.username = process.env.DW_USERNAME || instance.username || argv.username;
Expand Down
6 changes: 3 additions & 3 deletions commands/activate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import log from '../lib/log.js';
* @param {import('../index.js').DWArgv} argv
*/
export default async (argv) => {
const {clientId, clientPassword, hostname, apiVersion, codeVersion} = argv;
const { clientId, clientPassword, hostname, apiVersion, codeVersion } = argv;
log.info(`Activating ${codeVersion} on ${hostname}`);
const spinner = ora().start();

try {
const method = 'PATCH';
const endpoint = `https://${hostname}/s/-/dw/data/${apiVersion}/code_versions/${codeVersion}`;
const body = {active: true};
const body = { active: true };
spinner.text = 'Activating';
await api({clientId, clientPassword, method, endpoint, body});
await api({ clientId, clientPassword, method, endpoint, body });
spinner.succeed();
log.success('Success');
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions commands/clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import api from '../lib/api.js';
* @param {import('../index.js').DWArgv} argv
*/
export default async (argv) => {
const {clientId, clientPassword, hostname, apiVersion, webdav, request} =
const { clientId, clientPassword, hostname, apiVersion, webdav, request } =
argv;
log.info(`Cleaning up ${webdav}`);
const spinner = ora();
Expand All @@ -16,7 +16,7 @@ export default async (argv) => {
spinner.text = 'Reading';
const method = 'get';
const endpoint = `https://${hostname}/s/-/dw/data/${apiVersion}/code_versions`;
const {data} = await api({clientId, clientPassword, method, endpoint});
const { data } = await api({ clientId, clientPassword, method, endpoint });
if (data.length === 1) {
spinner.text = `Already clean`;
spinner.succeed();
Expand Down
2 changes: 1 addition & 1 deletion commands/extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import unzip from '../lib/unzip.js';
import log from '../lib/log.js';

export default async (argv) => {
const {file, request} = argv;
const { file, request } = argv;
log.info(`Extracting ${file}`);
const spinner = ora();

Expand Down
2 changes: 1 addition & 1 deletion commands/init.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path';
import fs from 'fs-extra';
import {fileURLToPath} from 'node:url';
import { fileURLToPath } from 'node:url';
import log from '../lib/log.js';

// @ts-ignore
Expand Down
6 changes: 3 additions & 3 deletions commands/job.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import api from '../lib/api.js';
import log from '../lib/log.js';

export default async (argv) => {
const {clientId, clientPassword, hostname, apiVersion, jobId} = argv;
const { clientId, clientPassword, hostname, apiVersion, jobId } = argv;
log.info(`Running job ${jobId} on ${hostname}`);
const spinner = ora().start();

try {
const endpoint = `https://${hostname}/s/-/dw/data/${apiVersion}/jobs/${jobId}/executions`;
const {id} = await api({
const { id } = await api({
clientId,
clientPassword,
method: 'post',
Expand All @@ -18,7 +18,7 @@ export default async (argv) => {
});

do {
var {status} = await api({
var { status } = await api({
clientId,
clientPassword,
method: 'get',
Expand Down
8 changes: 4 additions & 4 deletions commands/keygen.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import log from '../lib/log.js';
import {execSync} from 'node:child_process';
import { execSync } from 'node:child_process';

export default function (argv) {
const {user, crt, key, srl, days} = argv;
const { user, crt, key, srl, days } = argv;
log.info(
`Generating a staging certificate for stage instance user account ${user}`
);
Expand All @@ -23,11 +23,11 @@ export default function (argv) {

const userKeyCommand = `openssl req -new -newkey rsa:2048 -nodes -out ${user}.req -keyout ${user}.key -subj "/C=CO/ST=State/L=Local/O=Demandware/OU=Technology/CN=${user}"`;
log.info(userKeyCommand);
execSync(userKeyCommand, {encoding: 'utf8'});
execSync(userKeyCommand, { encoding: 'utf8' });

const signCommand = `openssl x509 -CA '${crt}' -CAkey '${key}' -CAserial '${srl}' -req -in ${user}.req -out ${user}.pem -days ${days}`;
log.info(signCommand);
execSync(signCommand, {encoding: 'utf8'});
execSync(signCommand, { encoding: 'utf8' });

log.success('Files generated.');
}
14 changes: 7 additions & 7 deletions commands/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import find from '../lib/find.js';
debug('log');

export default async (argv) => {
const {webdav, request, options} = argv;
const { webdav, request, options } = argv;
const verb = options.search ? 'Searching' : 'Streaming';
const text =
`${verb} log files from ${webdav} ` +
Expand All @@ -36,12 +36,12 @@ export default async (argv) => {
let files = await find('Logs', request);

// only log files
files = files.filter(({displayname}) => displayname.includes('.log'));
files = files.filter(({ displayname }) => displayname.includes('.log'));

// group by log type
let groups = groupBy(
files,
({displayname}) => displayname.split('-blade')[0]
({ displayname }) => displayname.split('-blade')[0]
);

if (options.list) {
Expand Down Expand Up @@ -93,7 +93,7 @@ export default async (argv) => {
const displayname = file.displayname;
try {
const response = await read(`Logs/${displayname}`, request);
return {response, name};
return { response, name };
} catch (error) {
output(() => log.error(error));
}
Expand All @@ -103,7 +103,7 @@ export default async (argv) => {
for (const promises of promiseGroups) {
const results = await Promise.all(promises);

for (const {response, name} of compact(results)) {
for (const { response, name } of compact(results)) {
let lines = response.split('\n');
lines.pop(); // last line is empty
if (options.numLines) lines = lines.slice(-options.numLines);
Expand Down Expand Up @@ -159,15 +159,15 @@ export default async (argv) => {
const displayname = files[0].displayname;
try {
const response = await read(`Logs/${displayname}`, request);
return {response, name};
return { response, name };
} catch (error) {
output(() => log.error(error));
}
});

const results = await Promise.all(promises);

for (const {response, name} of compact(results)) {
for (const { response, name } of compact(results)) {
let lines = response.split('\n');
lines.pop(); // last line is empty
if (options.numLines) lines = lines.slice(-options.numLines);
Expand Down
14 changes: 8 additions & 6 deletions commands/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import fs from 'fs-extra';
import path from 'node:path';
import ora from 'ora';
import chalk from 'chalk';
import {globby} from 'globby';
import {get} from 'lodash-es';
import { globby } from 'globby';
import { get } from 'lodash-es';
import notifier from 'node-notifier';
import zip from '../lib/zip.js';
import unzip from '../lib/unzip.js';
Expand All @@ -16,7 +16,7 @@ import log from '../lib/log.js';
import find from '../lib/find.js';

export default async (options) => {
const {cartridges, codeVersion, webdav, request} = options;
const { cartridges, codeVersion, webdav, request } = options;

try {
fs.accessSync(cartridges);
Expand All @@ -43,13 +43,15 @@ export default async (options) => {

spinner.start();
spinner.text = `Cleaning remote folder ${destination}`;
let files = (await find(destination, request))
let files = await find(destination, request);
files = files
.map((file) => file.displayname)
.filter((file) => file !== codeVersion);
await Promise.all(
files.map((file) => del(path.join(destination, file), request))
);
files = (await find(destination, request))
files = await find(destination, request);
files = files
.map((file) => file.displayname)
.filter((file) => file !== codeVersion);
await Promise.all(
Expand All @@ -62,7 +64,7 @@ export default async (options) => {
spinner.start();
spinner.text = `Uploading ${destination}/archive.zip`;
zipped = await write(zipped, destination, request, {
onProgress({percent, total, transferred}) {
onProgress({ percent, total, transferred }) {
const sizeInMegabytes = (total / 1_000_000).toFixed(2);
const uploadedInMegabytes = (transferred / 1_000_000).toFixed(2);
const prettyPercent = chalk.yellow.bold(`${percent}%`);
Expand Down
2 changes: 1 addition & 1 deletion commands/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import del from '../lib/delete.js';
import log from '../lib/log.js';

export default async (argv) => {
const {codeVersion, webdav, request} = argv;
const { codeVersion, webdav, request } = argv;
log.info(`Removing ${codeVersion} from ${webdav}`);
const spinner = ora();

Expand Down
6 changes: 3 additions & 3 deletions commands/versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import log from '../lib/log.js';
* @param {import('../index.js').DWArgv} argv
*/
export default async (argv) => {
const {clientId, clientPassword, hostname, apiVersion} = argv;
const { clientId, clientPassword, hostname, apiVersion } = argv;
log.info(`Reading code versions on ${hostname}`);
const spinner = ora().start();

try {
spinner.text = 'Reading';
const method = 'GET';
const endpoint = `https://${hostname}/s/-/dw/data/${apiVersion}/code_versions`;
await api({clientId, clientPassword, method, endpoint});
const data = await api({clientId, clientPassword, method, endpoint});
await api({ clientId, clientPassword, method, endpoint });
const data = await api({ clientId, clientPassword, method, endpoint });
// spinner.succeed();
log.plain('-------------------');
for (const version of data) {
Expand Down
Loading

0 comments on commit 1e303f5

Please sign in to comment.