From 84457ce6d1d03707e65919ee7a1b5624e6a4dc2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20=C4=86wirko?= Date: Sun, 8 Aug 2021 20:36:27 +0200 Subject: [PATCH] custom templates --- CHANGELOG.md | 4 ++++ README.md | 32 +++++++++++++++++++++++++++++--- bin/create-harold-app.js | 9 ++++++--- bin/packagejson.template | 2 +- package-lock.json | 2 +- package.json | 2 +- 6 files changed, 42 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6a9cec..916f051 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### [0.7.0](https://github.com/juliancwirko/create-harold-app/releases/tag/v0.7.0) (2021-08-08) + +- possibility to pass a custom template package when initializing the project + ### [0.6.0](https://github.com/juliancwirko/create-harold-app/releases/tag/v0.6.0) (2021-07-11) - new docs template as another option `-t docs` diff --git a/README.md b/README.md index e5f6685..daf6e9c 100644 --- a/README.md +++ b/README.md @@ -48,10 +48,36 @@ As an option, you can choose with which template it should init the project. Pos If you want to init the project with `docs` template, pass additional option `-t docs`. For example: `npm init harold-app my-app -t docs` or with npm 7+ `npm init harold-app my-app -- -t docs`. The same for `bare` template. -In the future, there will be a possibility to pass custom templates. - Write `create-harold-app --help` in a terminal to get the list of options. +### Custom templates + +You can prepare your template and reuse it on every new project initialization. You can do this by passing a path to your zip package. It can be a remote, hosted file or one from a local file system. + +Examples: + +``` +npm init harold-app my-app -t https://github.com/juliancwirko/harold-template-scaffold/archive/refs/heads/main.zip +``` + +with npm 7+ +``` +npm init harold-app my-app -- -t https://github.com/juliancwirko/harold-template-scaffold/archive/refs/heads/main.zip +``` + +Or it can be also local file: + +``` +npm init harold-app my-app -t ./main.zip +``` + +with npm 7+ +``` +npm init harold-app my-app -- -t ./main.zip +``` + +Read more about it in the docs here: [Custom templates](https://www.haroldjs.com/docs/custom-templates.html). + ### Starting the app From the newly created app's directory (in our case, `my-app`), run `npm start`. It will serve the app under `localhost:3000`. To change the port, just add `PORT` env, like: `PORT=3002 npm start`. @@ -69,7 +95,7 @@ You can configure the directory for md files (by default `posts`) and the direct You can also configure the name for output directory using `outputDirName` and if you want to host your site in subdirectory you would also need to add `hostDirName`. -Example of `.haroldrc` (placed in the root of your harold app): +Example of `.haroldrc` (placed in the root of the app or in the src directory): ``` { diff --git a/bin/create-harold-app.js b/bin/create-harold-app.js index fe9b1ca..52dc8d8 100755 --- a/bin/create-harold-app.js +++ b/bin/create-harold-app.js @@ -93,7 +93,8 @@ if (options.template && Object.keys(TEMPLATES).includes(options.template)) { if (isUrl(templateArchiveFilePath)) { // Extract required files to proper directories // This should be an archive link from a repository - // We need to strip one level of directories in the archive + // Assumption: you have root directory with files in the archive, by default in repository package + // so, we need to strip one level of directories in the archive download( templateArchiveFilePath, process.cwd() + '/' + projectName + '/src', @@ -114,10 +115,12 @@ if (isUrl(templateArchiveFilePath)) { }); } else { // This should be an archive file from local path - // Assumed that we don't have files on one root level directory + // Assumption: you have root directory with files in the archive + // so, we need to strip one level of directories in the archive decompress( templateArchiveFilePath, - process.cwd() + '/' + projectName + '/src' + process.cwd() + '/' + projectName + '/src', + { strip: 1 } ) .then(projectInit) .catch((err) => { diff --git a/bin/packagejson.template b/bin/packagejson.template index ff9c56a..e7c3602 100644 --- a/bin/packagejson.template +++ b/bin/packagejson.template @@ -8,6 +8,6 @@ "start": "harold-scripts start" }, "devDependencies": { - "harold-scripts": "^0.6.0" + "harold-scripts": "^0.7.0" } } diff --git a/package-lock.json b/package-lock.json index 0ef929f..aac0ef9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "create-harold-app", - "version": "0.6.0", + "version": "0.7.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 27bd25d..0b591d8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "create-harold-app", - "version": "0.6.0", + "version": "0.7.0", "description": "Static blog/site generator", "author": "Julian Ćwirko (julian.io)", "license": "MIT",