Skip to content

Commit 94b1075

Browse files
committed
code: new command create
1 parent 782cbd2 commit 94b1075

4 files changed

Lines changed: 137 additions & 4 deletions

File tree

cmds/core/create.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Copyright (C) 2022 Jen-Chieh Shen
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 3, or (at your option)
7+
* any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with GNU Emacs; see the file COPYING. If not, write to the
16+
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17+
* Boston, MA 02110-1301, USA.
18+
*/
19+
20+
"use strict";
21+
22+
const init = require('./init');
23+
const child_process = require('child_process');
24+
25+
exports.command = ['create [name]', 'new [name]'];
26+
exports.desc = 'create a new elisp project';
27+
exports.builder = {
28+
name: {
29+
description: 'new project name',
30+
requiresArg: false,
31+
type: 'string',
32+
},
33+
};
34+
35+
const TEMPLATE_URL = 'https://github.com/emacs-eask/template-elisp';
36+
37+
exports.handler = async (argv) => {
38+
const project_name = argv.name;
39+
40+
let proc = child_process.spawn('git', ['clone', TEMPLATE_URL, project_name],
41+
{ stdio: 'inherit' });
42+
43+
// You would just need to register the error event, or else it can't print
44+
// the help instruction below.
45+
proc.on('error', function () { });
46+
47+
proc.on('close', function (code) {
48+
if (code == 0) {
49+
console.log('✓ Done cloning the template project');
50+
process.chdir(project_name);
51+
cloned(argv);
52+
return;
53+
}
54+
// Help instruction here!
55+
console.log('✗ Error while cloning template project');
56+
console.log('');
57+
console.log(' [1] Make sure you have git installed and has the right permission');
58+
process.stdout.write(` [2] Failed because of the target directory isn't empty`);
59+
});
60+
};
61+
62+
63+
/* Operations after cloned */
64+
async function cloned(argv) {
65+
await init.create_eask_file();
66+
UTIL.e_call(argv, 'core/create');
67+
}

cmds/core/init.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,26 @@ const path = require('path');
2323
const fs = require('fs');
2424
const readline = require('readline');
2525

26-
const EASK_FILE = path.join(process.cwd(), '/Eask');
26+
var EASK_FILE;
2727

2828
var instance; /* `readline` instance */
2929

3030
exports.command = ['init'];
3131
exports.desc = 'create new Eask file in current directory';
3232

3333
exports.handler = async ({}) => {
34+
await create_eask_file();
35+
};
36+
37+
async function create_eask_file(dir) {
3438
let basename = path.basename(process.cwd());
3539
instance = readline.createInterface({
3640
input: process.stdin,
3741
output: process.stdout
3842
});
3943

44+
EASK_FILE = path.join(process.cwd(), '/Eask');
45+
4046
if (fs.existsSync(EASK_FILE)) {
4147
console.log('Eask file is already exists');
4248
process.exit(0);
@@ -71,7 +77,7 @@ Is this OK? (yes) `,
7177
}
7278
});
7379
instance.close();
74-
};
80+
}
7581

7682
/*
7783
* Ask question with callback
@@ -83,3 +89,5 @@ function ask(question, callback) {
8389
instance.question(question, (answer) => { callback(answer); resolve(); });
8490
});
8591
}
92+
93+
module.exports.create_eask_file = create_eask_file;

cmds/util/upgrade-eask.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ exports.handler = async (argv) => {
4040
// Help instruction here!
4141
console.log('✗ Failed to upgrade Eask, possible causes are:');
4242
console.log('');
43-
console.log(' [1] Make sure you have git installed');
44-
console.log(' [2] You probably have installed Eask with npm, try');
43+
console.log(' [1] Make sure you have git installed and has the right permission');
44+
console.log(' [2] You probably have installed Eask with npm, try the following command:');
4545
console.log('');
4646
process.stdout.write(' $ npm install -g emacs-eask/eask@latest');
4747
});

lisp/core/create.el

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
;;; create.el --- create a new elisp project -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
;;
5+
;; Create a new elisp project,
6+
;;
7+
;; $ eask create [name]
8+
;;
9+
;;
10+
;; Initialization options:
11+
;;
12+
;; [name] new project name
13+
;;
14+
15+
;;; Code:
16+
17+
(load (expand-file-name
18+
"../_prepare.el"
19+
(file-name-directory (nth 1 (member "-scriptload" command-line-args))))
20+
nil t)
21+
22+
(defconst eask--template-project-name "template-elisp"
23+
"Holds template project name.")
24+
25+
(defun eask--replace-string-in-buffer (old new)
26+
"Replace OLD to NEW in buffer."
27+
(let ((str (buffer-string)))
28+
(setq str (s-replace old new str))
29+
(delete-region (point-min) (point-max))
30+
(insert str)))
31+
32+
;; XXX: we can't use `user-full-name' in batch-mode. It will always return
33+
;; empty string.
34+
(defun eask--get-user ()
35+
"Return user name."
36+
(string-trim (shell-command-to-string "git config user.name")))
37+
38+
;; XXX: we can't use `user-mail-address' in batch-mode. It will always return
39+
;; empty string.
40+
(defun eask--get-mail ()
41+
"Return user email."
42+
(string-trim (shell-command-to-string "git config user.email")))
43+
44+
(eask-start
45+
(let ((template-package-file (expand-file-name (concat eask--template-project-name ".el"))))
46+
(rename-file template-package-file eask-package-file)
47+
(with-current-buffer (find-file eask-package-file)
48+
(eask--replace-string-in-buffer eask--template-project-name (eask-package-name))
49+
(eask--replace-string-in-buffer "{ SUMMARY }" (eask-package-description))
50+
(eask--replace-string-in-buffer "{ YEAR }" (format-time-string "%Y"))
51+
(eask--replace-string-in-buffer "{ FULL_NAME }" (eask--get-user))
52+
(eask--replace-string-in-buffer "{ MAIL_ADDR }" (eask--get-mail))
53+
(save-buffer))
54+
(eask-msg "✓ Congratulations! Your new Elisp project is created in %s" eask-file-root)
55+
(eask-msg "")
56+
(eask-msg "Visit https://emacs-eask.github.io/ for quickstart guide and full documentation.")))
57+
58+
;;; create.el ends here

0 commit comments

Comments
 (0)