Skip to content

Commit

Permalink
refactor: code base
Browse files Browse the repository at this point in the history
  • Loading branch information
Airkro committed Oct 19, 2023
1 parent adb4eea commit ba6dac6
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 105 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/npm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bring-it/npm",
"version": "0.3.7",
"version": "0.3.8",
"description": "Publish npm packages when needed",
"license": "MIT",
"author": {
Expand Down
2 changes: 1 addition & 1 deletion packages/sample/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bring-it/sample",
"version": "0.2.5",
"version": "0.2.6",
"description": "Generate code sample files",
"license": "MIT",
"author": {
Expand Down
2 changes: 1 addition & 1 deletion packages/sentry/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bring-it/sentry",
"version": "0.1.2",
"version": "0.1.3",
"description": "Update sentry artifacts",
"license": "MIT",
"author": {
Expand Down
44 changes: 5 additions & 39 deletions packages/sftp/lib/cmd.mjs
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
import { resolve } from 'node:path';
import { pathToFileURL } from 'node:url';

import { logger } from './sftp/logger.mjs';
import { checkSource } from './sftp/prepare.mjs';
import { checkServer, preset } from './sftp/read-config.mjs';
import { preset } from './sftp/lib.mjs';

export const command = 'sftp [server]';

export const describe = 'SFTP deployment command';

function parsePath(cwd = preset.cwd) {
return resolve(
process.cwd(),
cwd.replaceAll(/[/\\]+/g, '/').replaceAll(/^\/|\/$/g, ''),
);
}

const {
SSH_PRIVATE_KEY_PATH,
ssh_private_key_path = SSH_PRIVATE_KEY_PATH,
Expand All @@ -30,14 +18,12 @@ export function builder(cli) {
'URI as user@hostname[:port][/path]',
`or Host section in '${preset.conf}'`,
].join('\r\n'),
coerce: checkServer,
})
.options({
cwd: {
alias: 'c',
description: `default: ${preset.cwd}`,
requiresArg: true,
coerce: (cwd) => parsePath(cwd),
},
key: {
alias: 'k',
Expand All @@ -53,36 +39,16 @@ export function builder(cli) {
});
}

export function handler({
cwd = parsePath(),
key = private_key_path,
server,
path: forcePath,
}) {
export function handler({ cwd, key = private_key_path, server, path }) {
if (!server) {
throw new Error('Missing required positional: server');
}

const { user, hostname, port, path: filePath } = server;

const path = forcePath ?? filePath;

if (!key) {
throw new Error('Missing required argument: key');
}

if (checkSource(cwd)) {
logger.info('From:', pathToFileURL(cwd).toString());
logger.info('To:', `sftp://${user}@${hostname}:${port}${path}`);

const options = { hostname, port, user, key };

import('./sftp/action.mjs')
.then(({ action }) => action(options, { cwd, path }))
.catch(console.error);
}
import('./sftp/action.mjs')
.then(({ action }) => action({ cwd, path, key }))
.catch(console.error);
}

process.on('SIGINT', () => {
process.exit(1);
});
40 changes: 36 additions & 4 deletions packages/sftp/lib/sftp/action.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@
export async function action(options, { cwd, path }) {
const { SSH } = await import('./ssh.mjs');
const { upload } = await import('./upload.mjs');
import { resolve } from 'node:path';
import { pathToFileURL } from 'node:url';

return SSH(options, (ssh) => upload(ssh, cwd, path));
import { preset } from './lib.mjs';
import { logger } from './logger.mjs';
import { checkSource } from './prepare.mjs';
import { checkServer } from './read-config.mjs';

function parsePath(cwd = preset.cwd) {
return resolve(
process.cwd(),
cwd.replaceAll(/[/\\]+/g, '/').replaceAll(/^\/|\/$/g, ''),
);
}

export async function action({ key, cwd, server, path: forcePath }) {
const { user, hostname, port, path: filePath } = checkServer(server);

const path = forcePath ?? filePath;

const CWD = parsePath(cwd);

if (checkSource(CWD)) {
logger.info('From:', pathToFileURL(CWD).toString());
logger.info('To:', `sftp://${user}@${hostname}:${port}${path}`);

const options = { hostname, port, user, key };

const { SSH } = await import('./ssh.mjs');
const { upload } = await import('./upload.mjs');

return SSH(options, (ssh) => upload(ssh, CWD, path));
}
}

process.on('SIGINT', () => {
process.exit(1);
});
4 changes: 4 additions & 0 deletions packages/sftp/lib/sftp/lib.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const preset = {
cwd: '.bring-it',
conf: '.ssh/config',
};
5 changes: 1 addition & 4 deletions packages/sftp/lib/sftp/read-config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { readFileSync } from 'node:fs';
import pickBy from 'lodash/pickBy.js';
import SSHConfig from 'ssh-config';

export const preset = {
cwd: '.bring-it',
conf: '.ssh/config',
};
import { preset } from './lib.mjs';

function paramsError(message) {
throw new Error(`Error: ${message}`);
Expand Down
2 changes: 1 addition & 1 deletion packages/sftp/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bring-it/sftp",
"version": "0.1.6",
"version": "0.1.7",
"description": "SFTP deployment tool for frontend",
"license": "MIT",
"author": {
Expand Down
49 changes: 49 additions & 0 deletions packages/utils/action/pack.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { execFile } from 'node:child_process';

import { ignore } from '@bring-it/utils';
import { globbySync } from 'globby';

function checkTarget(pattern) {
if (pattern.length === 0) {
throw new Error('Error: no pattern input');
}

const list = globbySync(pattern, {
dot: true,
ignore,
});

if (list.length > 0) {
return pattern;
}

throw new Error('Error: no files targeting');
}

export async function action({ pattern, name }) {
const target = checkTarget(pattern);

let io;

execFile('tar', ['-c', '-f', `${name}.tar`, ...target], (error) => {
if (error) {
io = error;
}
});

execFile('tar', ['-c', '-f', `${name}.tar.gz`, '-z', ...target], (error) => {
if (error) {
io = error;
}
});

execFile('zip', ['-r', '-q', `${name}.zip`, ...target], (error) => {
if (error) {
io = error;
}
});

if (io) {
throw new Error(`Error: ${io.message}`);
}
}
56 changes: 6 additions & 50 deletions packages/utils/cmd/pack.mjs
Original file line number Diff line number Diff line change
@@ -1,36 +1,12 @@
import { execFile } from 'node:child_process';

import { ignore } from '@bring-it/utils';
import { globbySync } from 'globby';

export const command = 'pack [target...]';
export const command = 'pack [pattern...]';

export const describe = 'Pack files when support';

function checkTarget(pattern) {
if (pattern.length === 0) {
throw new Error('Error: no pattern input');
}

const list = globbySync(pattern, {
dot: true,
ignore,
});

if (list.length > 0) {
return pattern;
}

throw new Error('Error: no files targeting');
}

export function builder(cli) {
cli
.positional('target', {
.positional('pattern', {
description: 'glob pattern of files or directories',
coerce: checkTarget,
})
.default('target')
.options({
name: {
alias: 'n',
Expand All @@ -40,28 +16,8 @@ export function builder(cli) {
});
}

export function handler({ target = [], name }) {
let io;

execFile('tar', ['-c', '-f', `${name}.tar`, ...target], (error) => {
if (error) {
io = error;
}
});

execFile('tar', ['-c', '-f', `${name}.tar.gz`, '-z', ...target], (error) => {
if (error) {
io = error;
}
});

execFile('zip', ['-r', '-q', `${name}.zip`, ...target], (error) => {
if (error) {
io = error;
}
});

if (io) {
throw new Error(`Error: ${io.message}`);
}
export function handler(io) {
import('../action/pack.mjs')
.then(({ action }) => action(io))
.catch(console.error);
}

0 comments on commit ba6dac6

Please sign in to comment.