Skip to content

Commit

Permalink
fix: Allow initialize in other scopes (#229)
Browse files Browse the repository at this point in the history
* fix: Allow initialize in other scope

* Update docs

* changelog
  • Loading branch information
jcs090218 committed Feb 23, 2024
1 parent a5568f2 commit 9442844
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 190 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
* chore: Alias `pack` to `package` command (e209d7c8152a17de81613f09b380a2f5ad05697a)
* fix(build.yml): Release with `.tar.gz` instead of `.zip` on Unix-like system (#214 and #216)
* feat: Add `loc` command (#227)
* fix: Allow initialize in other scopes (#229)

## 0.9.x
> Released Nov 17, 2023
Expand Down
177 changes: 1 addition & 176 deletions cmds/core/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,181 +54,6 @@ exports.handler = async (argv) => {
break;
}
} else {
await create_eask_file();
await UTIL.e_call(argv, 'core/init');
}
};

var instance; /* `readline` instance */

async function create_eask_file(dir) {
let basename = path.basename(process.cwd());
instance = readline.createInterface({
input: process.stdin,
output: process.stdout
});

let new_name = path.join(process.cwd(), 'Eask');

// Search for existing Eask-files!
let files = fs.readdirSync(process.cwd()).filter(fn => fn.match('Eask'));
let continue_op = false;

if (files.length != 0) {
// Print out all existing Eask-files, and ask for continuation!
console.log('Eask-file is already exists,');
console.log('');
for (let index in files) {
console.log(' ' + path.join(process.cwd(), files[index]));
}
console.log('');
await ask(`Continue the creation? (yes) `, (answer) => { continue_op = answer; });

// Abort if declined!
if (continue_op != '' && continue_op != 'yes') {
process.exit(0);
}

// Ask for new name unitl the filename is available!
let new_basename = path.basename(new_name);
let invalid_name = false;

// Ask for new name until we found one that meets our requirements!
while (fs.existsSync(new_name) || invalid_name) {
let prompt;

// Handle invalid file name!
if (invalid_name) {
prompt = `[?] Invalid filename '${new_basename}', `;
} else {
prompt = `[?] Filename '${new_basename}' already taken, `;
}

// Ask for new name!
await ask(prompt + `try another one: `,
(answer) => {
new_name = path.join(process.cwd(), answer);
new_basename = answer;
invalid_name = !check_eask_filename(answer);
});
}
}

// Starting writing Eask-file!
let name, version, description, entry_point, emacs_version, website_url, keywords;

let guessed_package_name = guess_package_name(basename);
let guessed_entry_point = guess_entry_point(basename);

await ask(`package name: (${guessed_package_name}) `, (answer) => {
name = answer || guessed_package_name;
});
await ask(`version: (1.0.0) `, (answer) => { version = answer || '1.0.0'; });
await ask(`description: `, (answer) => { description = answer; });
await ask(`entry point: (${guessed_entry_point}) `, (answer) => {
entry_point = answer || guessed_entry_point;
});
await ask(`emacs version: (26.1) `, (answer) => { emacs_version = answer || '26.1'; });
await ask(`website: `, (answer) => { website_url = answer; });
await ask(`keywords: `, (answer) => { keywords = answer; });

name = remove_double_quotes(name);
version = remove_double_quotes(version);
description = remove_double_quotes(description);
entry_point = remove_double_quotes(entry_point);
emacs_version = remove_double_quotes(emacs_version);
website_url = remove_double_quotes(website_url);
keywords = remove_double_quotes(keywords);

keywords = keywords.split(/[, ]+/).join('" "');

let content = `(package "${name}"
"${version}"
"${description}")
(website-url "${website_url}")
(keywords "${keywords}")
(package-file "${entry_point}")
(script "test" "echo \\\"Error: no test specified\\\" && exit 1")
(source "gnu")
(depends-on "emacs" "${emacs_version}")
`;

await ask(`About to write to ${new_name}:
${content}
Is this OK? (yes) `,
(answer) => {
if (answer == '' || answer == 'yes') {
fs.writeFile(new_name, content, (err) => { if (err) console.log(err); });
}
});
instance.close();
}

/**
* Ask question with callback
* @param { string } question - question to ask for user input
* @param { callback } callback - callback with user's answer
*/
function ask(question, callback) {
return new Promise((resolve, reject) => {
instance.question(question, (answer) => { callback(answer); resolve(); });
});
}

/**
* Return true if NAME is a valid Eask-file filename.
* @param { string } name - either a filename or file path.
* @return { boolean } - True for valid filename.
*/
function check_eask_filename(name) {
let base = path.basename(name);
let prefix;
if (base.startsWith('Easkfile')) prefix = 'Easkfile';
else if (base.startsWith('Eask')) prefix = 'Eask';
else return false;
let suffix = base.replace(prefix, '');
return suffix == '' || /^[.][0-9]/.test(suffix);
}

/**
* Return the guessed project name by its basename.
* @param { String } basename - The working directory name.
* @return Guessed project name.
*/
function guess_package_name(basename) {
basename = basename.toLowerCase();
basename = basename.replaceAll('emacs-', '');
basename = basename.replaceAll('-emacs', '');
basename = basename.replace(/[-.]el$/, '');
return basename;
}

/**
* Return the guessed entry point by its basname.
* @param { String } basename - The working directory name.
* @return Guessed entry point filename.
*/
function guess_entry_point(basename) {
basename = guess_package_name(basename);
return basename + ".el";
}

/**
* Remove all double quotes from string.
* @param { String } str - String to remove all double quotes.
* @return String has been removed all double quotes.
*/
function remove_double_quotes(str) {
return str.replaceAll('\"', '');
}

/*
* Module Exports
*/
module.exports.create_eask_file = create_eask_file;
6 changes: 2 additions & 4 deletions cmds/create/elpa.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

const child_process = require('child_process');

const init = require('../core/init');

exports.command = ['elpa <name>'];
exports.desc = 'Create a new ELPA using github-elpa';
exports.builder = yargs => yargs
Expand Down Expand Up @@ -61,6 +59,6 @@ exports.handler = async (argv) => {
/* Operations after _cloned */
async function _cloned(argv) {
console.warn('Initialize the Eask-file for your project...');
await init.create_eask_file();
UTIL.e_call(argv, 'create/elpa');
await UTIL.e_call(argv, 'core/init');
await UTIL.e_call(argv, 'create/elpa');
}
6 changes: 2 additions & 4 deletions cmds/create/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

const child_process = require('child_process');

const init = require('../core/init');

exports.command = ['package <name>', 'pkg <name>'];
exports.desc = 'Create a new package';
exports.builder = yargs => yargs
Expand Down Expand Up @@ -61,6 +59,6 @@ exports.handler = async (argv) => {
/* Operations after _cloned */
async function _cloned(argv) {
console.warn('Initialize the Eask-file for your project...');
await init.create_eask_file();
UTIL.e_call(argv, 'create/package');
await UTIL.e_call(argv, 'core/init');
await UTIL.e_call(argv, 'create/package');
}
16 changes: 12 additions & 4 deletions docs/content/Development-API/_index.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ Points to `lisp` directory from the project root.
(message "%s" eask-lisp-root) ; path/to/eask/cli/lisp/
```

## 🔍 Function: eask-working-directory ()

Return the working directory of the program going to be executed.

```elisp
(message "%s" (eask-working-directory)) ; path/to/current/work/space/
```

## 🔍 Function: eask-command ()

Return the current command in string. Suppose the command is:
Expand Down Expand Up @@ -166,7 +174,7 @@ Call another eask script.

## 🔍 Function: eask-import (`url`)

Load and evaluate the script from a url.
Load and evaluate the script from the url.

```elisp
(eask-import "https://raw.githubusercontent.com/emacsmirror/emacswiki.org/master/yes-no.el")
Expand All @@ -176,7 +184,7 @@ Load and evaluate the script from a url.

## 🔍 Macro: eask-defvc< (`version` &rest `body`)

Define scope if Emacs version is below specific version.
Define the scope if the Emacs version is below a specific version.

`VERSION` is an integer and will be compared with `emacs-major-version`.

Expand Down Expand Up @@ -292,7 +300,7 @@ The following output is with Emacs 28.1:

Return a list of Eask files from DIR.

Consider following directory tree:
Consider the following directory tree:

```
. root
Expand All @@ -311,7 +319,7 @@ The following output is with Emacs 28.1:

Find the Eask-file from START-PATH.

Consider following directory tree:
Consider the following directory tree:

```
.project
Expand Down
8 changes: 8 additions & 0 deletions docs/content/Development-API/_index.zh-tw.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ $ eask <command> -- args0 args1
(message "%s" eask-lisp-root) ; path/to/eask/cli/lisp/
```

## 🔍 函式: eask-working-directory ()

傳回將要執行的程式的工作目錄。

```elisp
(message "%s" (eask-working-directory)) ; path/to/current/work/space/
```

## 🔍 函式: eask-command ()

返回字符串中的當前命令。假設命令是:
Expand Down
10 changes: 8 additions & 2 deletions lisp/_prepare.el
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ will return `lint/checkdoc' with a dash between two subcommands."
These commands will first respect the current workspace. If the current
workspace has no valid Eask-file; it will load global workspace instead."
(member (eask-command) '("init/cask" "init/eldev" "init/keg"
"init/source"
(member (eask-command) '("init" "init/source" "init/cask" "init/eldev" "init/keg"
"create/package" "create/elpa"
"bump" "cat" "keywords" "repl"
"generate/ignore" "generate/license"
"test/melpazoid")))
Expand Down Expand Up @@ -1023,6 +1023,12 @@ Argument BODY are forms for execution."
(defalias keyword (symbol-function old))))
result))

(defun eask-working-directory ()
"Return the working directory of the program going to be executed."
(cond ((eask-config-p) user-emacs-directory)
((eask-global-p) (expand-file-name "../../" user-emacs-directory))
(t default-directory)))

(defvar eask-file nil "The Eask file's filename.")
(defvar eask-file-root nil "The Eask file's directory.")

Expand Down
Loading

0 comments on commit 9442844

Please sign in to comment.