Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
na57 authored Apr 26, 2024
0 parents commit 6eabf3a
Show file tree
Hide file tree
Showing 12 changed files with 2,606 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = {
env: {
browser: true,
es2021: true,
},
extends: ["eslint:recommended", "prettier"],
overrides: [
{
env: {
node: true,
},
files: [".eslintrc.{js,cjs}"],
parserOptions: {
sourceType: "script",
},
},
],
parserOptions: {
ecmaVersion: "latest",
sourceType: "module",
},
rules: {},
};
31 changes: 31 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: Node.js CI

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x, 20.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'yarn'
- run: yarn
- run: npm run build --if-present
- run: yarn test
130 changes: 130 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 liudonghua

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# esm-template

[![Node.js CI](https://github.com/liudonghua123/esm-template/actions/workflows/node.js.yml/badge.svg)](https://github.com/liudonghua123/esm-template/actions/workflows/node.js.yml)

This is a simple NodeJS template project.

## Support features

- Default esm module support
- Type support in pure javascript
- Auto doc generation, need to write doc comments
- Format and Lint support

## TODOs

- [x] Add unit tests
- [x] Add github actions for testing


## License

MIT License

Copyright (c) 2024 liudonghua
9 changes: 9 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"module": "NodeNext",
"target": "ES6",
"checkJs": true,
"moduleResolution": "NodeNext"
},
"exclude": ["node_modules"]
}
44 changes: 44 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "nodejs-sandbox",
"version": "1.0.0",
"description": "",
"type": "module",
"main": "src/index.js",
"scripts": {
"start": "nodemon src/index.js",
"doc": "yarn jsdoc src",
"lint": "npx eslint src",
"lint:fix": "npm run lint -- --fix",
"prettier": "npx prettier --check src",
"prettier:fix": "npm run prettier -- --write",
"format": "npm run prettier:fix && npm run lint:fix",
"test": "node --test"
},
"repository": {
"type": "git",
"url": "git+https://github.com/liudonghua123/esm-template.git"
},
"keywords": [],
"author": "Ives van Hoorne",
"license": "MIT",
"bugs": {
"url": "https://github.com/liudonghua123/esm-template/issues"
},
"homepage": "https://github.com/liudonghua123/esm-template#readme",
"devDependencies": {
"eslint": "^7.32.0 || ^8.2.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.25.2",
"jsdoc": "^4.0.2",
"nodemon": "^3.0.1",
"prettier": "^3.2.5",
"typedoc": "^0.25.11"
},
"dependencies": {
"chalk": "^5.3.0",
"commander": "^12.0.0",
"figlet": "^1.7.0",
"inquirer": "^9.2.15",
"ora": "^8.0.1"
}
}
69 changes: 69 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env node

import process from "process";
import { program } from "commander";
import chalk from "chalk";
import inquirer from "inquirer";
import ora from "ora";
import figlet from "figlet";
import { say_hello, times_str } from "./util.js";

/**
* show the logo
* @returns {void}
*/
function show_logo() {
console.log(
chalk.yellow(figlet.textSync(say_hello(), { horizontalLayout: "full" })),
);
}

/**
* Process the choice according to the user's choice
* @param {{ choice: string }} result - the user's choice
* @returns {void}
*/
function process_choice(result) {
const spinner = ora(`Doing ${result.choice}...`).start(); // 启动旋转器动画
setTimeout(() => {
spinner.succeed(chalk.green("Done!")); // 3秒后完成,显示成功消息
}, 3000);
}

/**
* fetch data from the url
* @param {string} url - the url to fetch data
* @returns {Promise<Object>} the result
*/
async function fetch_demo(url = "https://api.github.com/users/github") {
const spinner = ora("Fetching data...").start();
const response = await fetch(url);
const data = await response.json();
spinner.succeed("Data fetched successfully!");
return data;
}

/**
* main function of the program
* @returns {Promise<void>} the result
*/
async function main() {
show_logo();
program.version("1.0.0").description("My Node CLI");
program.action(async () => {
const result = await inquirer.prompt([
{
type: "list",
name: "choice",
message: "Choose an option:",
choices: ["Option 1", "Option 2", "Option 3"],
},
]);
process_choice(result);
});
program.parse(process.argv);
console.info(`invoke times_str: ${times_str(3, "hello")}`);
console.info(`fetch some sample data: ${await fetch_demo()}`);
}

main();
22 changes: 22 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Build a simple greeting message
* @param {string} [message="hello"] - the message
* @returns {string} the greeting message
*/
export function say_hello(message = "hello") {
return `${message}, World!`;
}

/**
* Create times str
* @param {number} [count=1] - times
* @param {string} [message="hi"] - message
* @returns {string} the times message
*/
export function times_str(count = 1, message = "hi") {
let result = "";
for (let i = 0; i < count; i++) {
result += message;
}
return result;
}
Loading

0 comments on commit 6eabf3a

Please sign in to comment.