Skip to content

Commit

Permalink
feat: Merge run and command command (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 authored Oct 17, 2023
1 parent 1df7b6f commit d14d90e
Show file tree
Hide file tree
Showing 14 changed files with 158 additions and 115 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
* Add command to generate recipe (#192)
* Add option to init from `Eldev`-file (#193)
* Add command `commnad` for custom commands (#195)
* Merge the two commands `run` and `command` (#196)

## 0.8.x
> Released Mar 08, 2023
Expand Down
101 changes: 16 additions & 85 deletions cmds/core/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,91 +19,22 @@

"use strict";

const fs = require('fs');
const child_process = require("child_process");

exports.command = ['run [names..]', 'run-script [names..]'];
exports.desc = 'Run script nameds [names..]';
exports.builder = yargs => yargs
.positional(
'[names..]', {
description: 'specify scripts to run',
type: 'array',
});

exports.handler = async (argv) => {
await UTIL.e_call(argv, 'core/run', argv.names);

if (!fs.existsSync(EASK_HOMEDIR)) {
return;
}

let run = EASK_HOMEDIR + 'run';

if (!fs.existsSync(run)) {
return;
}

// this contain the full command!
let instruction = fs.readFileSync(run, 'utf8');
let commands = instruction.split('\n').filter(element => element);

let count = 0;
startCommand(commands, count);
};

/**
* Recursive to execute commands in order.
*
* @param { array } commands - An array of commands to execute.
* @param { integer } count - The current executing command's index.
*/
function startCommand(commands, count) {
if (commands.length <= count)
return;

let command = commands[count];

console.log('[RUN]: ' + command);

let proc = spawn(command, { stdio: 'inherit', shell: true });

proc.on('close', function (code) {
if (code == 0) {
startCommand(commands, ++count); // start next command!
return;
}
process.exit(code);
});
}

/**
* Spawn process to avoid `MODULE_NOT_FOUND` not found error,
* see https://github.com/vercel/pkg/issues/1356.
*
* @param { String } command - Command string.
* @param { JSON } options - Process options.
* @return Process object.
*/
function spawn(command, options) {
if (IS_PKG && command.includes('eask ')) {
let cmds = command.split(' ');
cmds = replaceEaskExec(cmds);
return child_process.spawn(process.execPath, cmds, options);
exports.command = ['run <type>'];
exports.desc = 'Run custom tasks';
exports.builder = function (yargs) {
yargs.usage(`${exports.desc}
Usage: eask run <type> [options..]`)
.commandDir('../run/')
.demandCommand();

/* XXX: Configure only in the menu. */
if (UTIL.cmd_count() == 1) {
yargs.positional(
'<type>', {
description: 'type of the execution',
});
}
return child_process.spawn(command, options);
}

/**
* Replace all possible eask/cli executable to snapshot executable.
* @param { Array } cmds - Command array.
* @return Return updated command array.
*/
function replaceEaskExec(cmds) {
for (let index = 0; index < cmds.length; ++index) {
if (cmds[index] == "eask") {
cmds[index] = process.argv[1]; // XXX: This is `/snapshot/cli/eask`
}
}
return cmds;
}
exports.handler = async (argv) => { };
2 changes: 1 addition & 1 deletion cmds/core/command.js → cmds/run/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ exports.builder = yargs => yargs
});

exports.handler = async (argv) => {
await UTIL.e_call(argv, 'core/command'
await UTIL.e_call(argv, 'run/command'
, argv.names);
};
109 changes: 109 additions & 0 deletions cmds/run/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* Copyright (C) 2022-2023 Jen-Chieh Shen
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Emacs; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/

"use strict";

const fs = require('fs');
const child_process = require("child_process");

exports.command = ['script [names..]'];
exports.desc = 'Run script nameds [names..]';
exports.builder = yargs => yargs
.positional(
'[names..]', {
description: 'specify scripts to run',
type: 'array',
});

exports.handler = async (argv) => {
await UTIL.e_call(argv, 'run/script', argv.names);

if (!fs.existsSync(EASK_HOMEDIR)) {
return;
}

let run = EASK_HOMEDIR + 'run';

if (!fs.existsSync(run)) {
return;
}

// this contain the full command!
let instruction = fs.readFileSync(run, 'utf8');
let commands = instruction.split('\n').filter(element => element);

let count = 0;
startCommand(commands, count);
};

/**
* Recursive to execute commands in order.
*
* @param { array } commands - An array of commands to execute.
* @param { integer } count - The current executing command's index.
*/
function startCommand(commands, count) {
if (commands.length <= count)
return;

let command = commands[count];

console.log('[RUN]: ' + command);

let proc = spawn(command, { stdio: 'inherit', shell: true });

proc.on('close', function (code) {
if (code == 0) {
startCommand(commands, ++count); // start next command!
return;
}
process.exit(code);
});
}

/**
* Spawn process to avoid `MODULE_NOT_FOUND` not found error,
* see https://github.com/vercel/pkg/issues/1356.
*
* @param { String } command - Command string.
* @param { JSON } options - Process options.
* @return Process object.
*/
function spawn(command, options) {
if (IS_PKG && command.includes('eask ')) {
let cmds = command.split(' ');
cmds = replaceEaskExec(cmds);
return child_process.spawn(process.execPath, cmds, options);
}
return child_process.spawn(command, options);
}

/**
* Replace all possible eask/cli executable to snapshot executable.
* @param { Array } cmds - Command array.
* @return Return updated command array.
*/
function replaceEaskExec(cmds) {
for (let index = 0; index < cmds.length; ++index) {
if (cmds[index] == "eask") {
cmds[index] = process.argv[1]; // XXX: This is `/snapshot/cli/eask`
}
}
return cmds;
}
4 changes: 2 additions & 2 deletions docs/content/en/Getting-Started/Basic-Usage/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ Usage: eask <command> [options..]
Commands:
archives List out all package archives [aliases: sources]
clean <type> Delete various files produced during building
command [names..] Run elisp commands named [names..] [aliases: cmd]
compile [names..] Byte compile all Emacs Lisp files in the package
create <type> Create a new elisp project
docker <version> [args..] Launch specified Emacs version in a Docker container
Expand All @@ -60,7 +59,7 @@ Commands:
recipe Suggest a recipe format
refresh Download package archives
reinstall [names..] Reinstall packages
run [names..] Run script nameds [names..] [aliases: run-script]
run <type> Run custom tasks
search [queries..] Search packages
status Display the state of the workspace
test <type> Run test
Expand Down Expand Up @@ -146,6 +145,7 @@ Here is a list of known nested subcommands:
- eask generate workflow
- eask link
- eask lint
- eask run
- eask test
## 📌 Knowing your `elpa` directory
Expand Down
10 changes: 4 additions & 6 deletions docs/content/en/Getting-Started/Commands-and-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,22 +316,20 @@ Evaluate `FORM` as a lisp form.
$ eask [GLOBAL-OPTIONS] eval [FORM]
```

## 🔍 eask run
## 🔍 eask run script

Run the script.

```sh
$ eask [GLOBAL-OPTIONS] run [NAMES..]
$ eask [GLOBAL-OPTIONS] run script [NAMES..]
```

Alias: `run-script`

## 🔍 eask command
## 🔍 eask run command

Run the command.

```sh
$ eask [GLOBAL-OPTIONS] command [NAMES..]
$ eask [GLOBAL-OPTIONS] run command [NAMES..]
```

Alias: `cmd`
Expand Down
4 changes: 2 additions & 2 deletions docs/content/zh-TW/Getting-Started/Basic-Usage/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ Usage: eask <command> [options..]
Commands:
archives List out all package archives [aliases: sources]
clean <type> Delete various files produced during building
command [names..] Run elisp commands named [names..] [aliases: cmd]
compile [names..] Byte compile all Emacs Lisp files in the package
create <type> Create a new elisp project
docker <version> [args..] Launch specified Emacs version in a Docker container
Expand All @@ -57,7 +56,7 @@ Commands:
recipe Suggest a recipe format
refresh Download package archives
reinstall [names..] Reinstall packages
run [names..] Run script nameds [names..] [aliases: run-script]
run <type> Run custom tasks
search [queries..] Search packages
status Display the state of the workspace
test <type> Run test
Expand Down Expand Up @@ -141,6 +140,7 @@ Positionals:
- eask generate workflow
- eask link
- eask lint
- eask run
- eask test
## 📌 了解你的 `elpa` 目錄
Expand Down
10 changes: 4 additions & 6 deletions docs/content/zh-TW/Getting-Started/Commands-and-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,22 +312,20 @@ $ eask [GLOBAL-OPTIONS] emacs [ARGUMENTS ...]
$ eask [GLOBAL-OPTIONS] eval [FORM]
```

## 🔍 eask run
## 🔍 eask run script

運行腳本。

```sh
$ eask [GLOBAL-OPTIONS] run [NAMES..]
$ eask [GLOBAL-OPTIONS] run script [NAMES..]
```

別名: `run-script`

## 🔍 eask command
## 🔍 eask run command

運行指令。

```sh
$ eask [GLOBAL-OPTIONS] command [NAMES..]
$ eask [GLOBAL-OPTIONS] run command [NAMES..]
```

別名: `cmd`
Expand Down
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions lisp/core/command.el → lisp/run/command.el
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
;;; core/command.el --- Run custom command -*- lexical-binding: t; -*-
;;; run/command.el --- Run custom command -*- lexical-binding: t; -*-

;;; Commentary:
;;
;; Command use to run custom command
;;
;; $ eask command [names..]
;; $ eask run command [names..]
;;
;;
;; Positionals:
Expand All @@ -25,7 +25,7 @@

(defun eask--print-commands ()
"Print all available commands."
(eask-msg "available via `eask command`")
(eask-msg "available via `eask run command`")
(eask-msg "")
(let* ((keys (reverse eask-commands))
(offset (eask-seq-str-max keys))
Expand All @@ -52,7 +52,7 @@
(eask-start
(cond ((null eask-commands)
(eask-info "(No command specified)")
(eask-help "core/command"))
(eask-help "run/command"))
((eask-all-p)
(dolist (name (reverse eask-commands))
(eask--execute-command name)))
Expand All @@ -69,4 +69,4 @@
t)))
(t (eask--print-commands))))

;;; core/command.el ends here
;;; run/command.el ends here
Loading

0 comments on commit d14d90e

Please sign in to comment.