Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release v1.7.0 #532

Merged
merged 19 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,27 @@ jobs:
with:
body: |
Please refer to [CHANGELOG.md](https://github.com/skyclouds2001/template-sky/blob/master/CHANGELOG.md) for details.
npm-release:
name: Publish npm release
if: github.repository == 'skyclouds2001/template-sky'
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/[email protected]
- name: Install pnpm
uses: pnpm/[email protected]
- name: Install Node.js
uses: actions/[email protected]
with:
registry-url: https://registry.npmjs.org/
node-version-file: .nvmrc
cache: pnpm
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Build package
run: pnpm build
- name: Publish package to npm
run: pnpm publish --no-git-checks
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
skyclouds2001 marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
npx lint-staged
npx lint-staged --allow-empty
32 changes: 32 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
# template-sky

## 1.7.0

### Minor Changes

- [`71be2ae`](https://github.com/skyclouds2001/template-sky/commit/71be2aef69d2584beb31d8c2ec4d1220c40bf103) Thanks [@skyclouds2001](https://github.com/skyclouds2001)! - fix: update bin cmd

- [`f86963b`](https://github.com/skyclouds2001/template-sky/commit/f86963ba6e34cd754ef458b24c1f9ae72192bc61) Thanks [@skyclouds2001](https://github.com/skyclouds2001)! - fix: change to use module generate result for cli generate

- [`ff37dd9`](https://github.com/skyclouds2001/template-sky/commit/ff37dd9267a4b68fcdd4f11b2ca265f6ffbebf42) Thanks [@skyclouds2001](https://github.com/skyclouds2001)! - feat: update template to latest version
skyclouds2001 marked this conversation as resolved.
Show resolved Hide resolved

### Patch Changes

- [`8fc5a5d`](https://github.com/skyclouds2001/template-sky/commit/8fc5a5d58017b42a479e4bf4c67164258f02dd8c) Thanks [@skyclouds2001](https://github.com/skyclouds2001)! - build: allow lint-staged pass empty commit

- [`f0160cc`](https://github.com/skyclouds2001/template-sky/commit/f0160cc6251a07d489799ee3cb2cf408c446eb8a) Thanks [@skyclouds2001](https://github.com/skyclouds2001)! - perf: migrate from using fs to fs/promises

- [`e184867`](https://github.com/skyclouds2001/template-sky/commit/e1848671f985a94e632273d38433f670ca1839a6) Thanks [@skyclouds2001](https://github.com/skyclouds2001)! - fix: better function name & file method call exception

- [`f19bd5e`](https://github.com/skyclouds2001/template-sky/commit/f19bd5ee34f20051d97e190d4ee6778ad63f4df6) Thanks [@skyclouds2001](https://github.com/skyclouds2001)! - docs: update for cli usage

- [`c73cf3d`](https://github.com/skyclouds2001/template-sky/commit/c73cf3db895283473e049a51dda1460ee03e6505) Thanks [@skyclouds2001](https://github.com/skyclouds2001)! - build: remove useless jsconfig.json file

- [`633f896`](https://github.com/skyclouds2001/template-sky/commit/633f896dbdc24e280605d0049bf958eb2a72af79) Thanks [@skyclouds2001](https://github.com/skyclouds2001)! - ci: run build before publish

- [`a9fcb44`](https://github.com/skyclouds2001/template-sky/commit/a9fcb4424c3ad3a8cf28922171487d6aafeccc9a) Thanks [@skyclouds2001](https://github.com/skyclouds2001)! - refactor: move rewrite file list

- [`3543a0f`](https://github.com/skyclouds2001/template-sky/commit/3543a0f00a16beb52eefe2e33dad92e305a9ce58) Thanks [@skyclouds2001](https://github.com/skyclouds2001)! - build: update engine limit

- [`d7a5e65`](https://github.com/skyclouds2001/template-sky/commit/d7a5e655f643599799b0453f236e242e6bcf298e) Thanks [@skyclouds2001](https://github.com/skyclouds2001)! - feat: log when rewrite file failed

- [`f3f48f7`](https://github.com/skyclouds2001/template-sky/commit/f3f48f751f0af7b9eb925c5c50005985952569a9) Thanks [@skyclouds2001](https://github.com/skyclouds2001)! - refactor: use class with static fields of template enum

## 1.6.0

### Major Changes
Expand Down
34 changes: 23 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,42 @@ An opinionated starter template for vite or rollup with a cli to generate templa

## Usage

Using npm to initialize a project
First install the package as a global dependency:

```sh
npx @sky-fly/template
npm i @sky-fly/template -g
```

You can pass one CLI option as the project name, which will be used to decide the folder name where the generated project template will be placed.
Then generate a project:

```sh
npx @sky-fly/template project
create
```

You can also pass another CLI option as the package name, which will be used to overwrite the default in the project. If not specified, the project name will be used as the package name.
Also directly use npx tool to generate a project without globally install it is available:

```sh
npx @sky-fly/template project project
npx @sky-fly/template init
```

Available named CLI options are shown below:
Can pass one CLI option as the project name, which will be used to decide the folder name where the generated project template will be placed.

| Name | Default | Type | Description |
| ------------------------ | ------- | ----------------------- | ------------------------------------------------------ |
| _--template_ _-t_ | - | `'vue'\|'react'` | decide the template the project is intended to use |
| _--package-manager_ _-p_ | - | `'npm'\|'yarn'\|'pnpm'` | decide the package manager the project is going to use |
```sh
create project-name
```

Can also pass another CLI option as the package name, which will be used to overwrite the default in the project. If not specified, the project name will be used as the package name.

```sh
create project-name package-name
```

Other available CLI options are shown below:

| Name | Default | Type | Description |
| ------------------------ | ------- | ------------------------------------ | ------------------------------------------------------ |
| _--template_ _-t_ | - | `'vite-vue'\|'vite-react'\|'rollup'` | decide the template the project is intended to use |
| _--package-manager_ _-p_ | - | `'npm'\|'yarn'\|'pnpm'` | decide the package manager the project is going to use |

## Changelog

Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
/* eslint-disable */
// @ts-nocheck

import './dist/index.cjs.js'
import './dist/index.es.js'
8 changes: 0 additions & 8 deletions jsconfig.json

This file was deleted.

13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "@sky-fly/template",
"version": "1.6.0",
"version": "1.7.0",
"description": "an opinionated starter template for vite or rollup with a cli to generate template project",
"keywords": [
"template",
"project",
"cli"
],
"bin": {
"create-template": "index.js"
"create": "index.js"
skyclouds2001 marked this conversation as resolved.
Show resolved Hide resolved
},
"files": [
"index.js",
Expand Down Expand Up @@ -57,12 +57,13 @@
},
"packageManager": "[email protected]",
"engines": {
"node": ">=18.13.0",
"npm": ">=9.6.0",
"yarn": ">=1.22.0",
"pnpm": ">=8.4.0"
"node": ">=20.0.0",
"npm": ">=10.0.0",
"yarn": ">=1.22.22",
"pnpm": ">=9.0.0"
skyclouds2001 marked this conversation as resolved.
Show resolved Hide resolved
},
"scripts": {
"init": "node ./index.js",
"dev": "rollup --config --watch",
"build": "rollup --config",
"lint": "eslint --fix . && prettier --write . && markdownlint --fix **/*.md && tsc --noEmit",
Expand Down
43 changes: 17 additions & 26 deletions src/fs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from 'node:fs'
import fs from 'node:fs/promises'
import path from 'node:path'

/**
Expand All @@ -11,25 +11,13 @@ export const IGNORE_CHECK = ['.git', '.vscode', '.idea', '.fleet']
*/
export const IGNORE_COPY = ['.git', 'node_modules', 'dist', '_']

/**
* files that should override project information (exclude package.json as it requires additional tasks to execute and README.md as it is not intended to edit)
*/
export const OVERRIDE_FILE = {
repository: ['.github/workflows/release.yml'],
userEmail: ['CODE_OF_CONDUCT.md', 'Dockerfile', 'index.html', 'SECURITY.md'],
description: ['index.html', 'public/site.webmanifest'],
keywords: ['index.html'],
userName: ['Dockerfile', 'index.html', 'LICENSE', '.changeset/config.json', '.github/dependabot.yml', '.github/ISSUE_TEMPLATE/bug-report.md', '.github/ISSUE_TEMPLATE/feature-request.md', '.github/ISSUE_TEMPLATE/other.md', '.github/workflows/ci.yml', '.github/workflows/labeler.yml', '.github/workflows/new-contributor.yml', '.github/workflows/project-automate.yml', '.github/workflows/release.yml', '.github/workflows/stale.yml'],
packageName: ['CHANGELOG.md', 'CONTRIBUTING.md', 'index.html', '.changeset/config.json', '.github/workflows/ci.yml', '.github/workflows/labeler.yml', '.github/workflows/new-contributor.yml', '.github/workflows/project-automate.yml', '.github/workflows/release.yml', '.github/workflows/stale.yml', 'public/site.webmanifest', 'tests/e2e/index.spec.ts'],
}

/**
* check if the target dictionary empty, will ignore those files in ignore list
* @param dir target dictionary
* @returns check result
*/
export function isEmptyDir(dir: string): boolean {
const files = fs.readdirSync(dir)
export async function isEmptyDir(dir: string): Promise<boolean> {
const files = await fs.readdir(dir)

return files.length === 0 || files.every((file) => IGNORE_CHECK.includes(file))
}
Expand All @@ -38,38 +26,41 @@ export function isEmptyDir(dir: string): boolean {
* clear the target dictionary to empty, will ignore those files in ignore list
* @param dir target dictionary
*/
export function clearDir(dir: string): void {
for (const file of fs.readdirSync(dir)) {
export async function clearDir(dir: string): Promise<void> {
const files = await fs.readdir(dir)

for (const file of files) {
if (IGNORE_CHECK.includes(file)) {
continue
}

fs.rmSync(path.resolve(dir, file), {
await fs.rmdir(path.resolve(dir, file), {
recursive: true,
force: true,
})
}
}

/**
* copy the file from the source to the target, will ignore those files in ignore list and existed in the target
* copy the file or dictionary from the source to the target, will ignore those files in ignore list and existed in the target
* @param src source path
* @param dest target path
*/
export function copy(src: string, dest: string): void {
if (fs.statSync(src).isDirectory()) {
fs.mkdirSync(dest, {
export async function copyDirOrFile(src: string, dest: string): Promise<void> {
const file = await fs.stat(src)

if (file.isDirectory()) {
await fs.mkdir(dest, {
recursive: true,
})

for (const file of fs.readdirSync(src)) {
for (const file of await fs.readdir(src)) {
if (IGNORE_COPY.includes(file)) {
continue
}

copy(path.resolve(src, file), path.resolve(dest, file))
await copyDirOrFile(path.resolve(src, file), path.resolve(dest, file))
}
} else {
fs.copyFileSync(src, dest)
await fs.copyFile(src, dest)
}
}
60 changes: 31 additions & 29 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import fs from 'node:fs'
import fs from 'node:fs/promises'
import path from 'node:path'
import url from 'node:url'
import kleur from 'kleur'
import minimist from 'minimist'
import prompts from 'prompts'
import { simpleGit } from 'simple-git'
import { clearDir, copy, isEmptyDir } from './fs'
import { clearDir, copyDirOrFile, isEmptyDir } from './fs'
import { PackageManager, getPackageManager, isValidPackageManagerName } from './package'
import { rewrite } from './rewrite'
import { isValidTemplateName, templates, type Template } from './template'
import { isValidTemplateName, Template } from './template'
import { isValidPackageName, isValidProjectName } from './validate'

const DEFAULT_NAME = 'template-sky'
Expand Down Expand Up @@ -65,7 +65,7 @@ const cli = async () => {
name: 'template',
message: 'Select a template:',
initial: 0,
choices: templates.map((template) => ({
choices: Object.values(Template).map((template) => ({
title: template.color(template.name),
value: template.name,
})),
Expand Down Expand Up @@ -97,39 +97,41 @@ const cli = async () => {
// check if the target dictionary is not an empty dictionary
// if so, prompt to let user decide whether overwrite the target dictionary
// if user decide to overwrite, clear the target dictionary; if not, exit the process
if (fs.existsSync(root) && !isEmptyDir(root)) {
const { overwrite } = await prompts(
[
try {
const shouldNotOverwrite = await isEmptyDir(root)

if (!shouldNotOverwrite) {
const { overwrite } = await prompts(
[
{
type: 'confirm',
name: 'overwrite',
message: `${root === __dirname ? 'Current' : 'Target'} dictionary is not empty, remove existing files and continue?`,
},
],
{
type: 'confirm',
name: 'overwrite',
message: `${root === __dirname ? 'Current' : 'Target'} dictionary is not empty, remove existing files and continue?`,
},
],
{
onCancel: () => {
throw new Error(`${kleur.red('✖')} Operation cancelled!`)
},
onCancel: () => {
throw new Error(`${kleur.red('✖')} Operation cancelled!`)
},
}
)

if (overwrite as boolean) {
await clearDir(root)
} else {
throw new Error(`${kleur.red('✖')} Operation cancelled!`)
}
skyclouds2001 marked this conversation as resolved.
Show resolved Hide resolved
)

if (overwrite as boolean) {
clearDir(root)
} else {
throw new Error(`${kleur.red('✖')} Operation cancelled!`)
}
}

// create target dictionary if it doesn't existed
if (!fs.existsSync(root)) {
fs.mkdirSync(root)
} catch {
// create target dictionary if it doesn't existed
await fs.mkdir(root, { recursive: true })
}

// get the template dictionary name
const templateDir = path.resolve(url.fileURLToPath(import.meta.url), '../..', (templates.find((f) => f.name === template) as Template).template)
const templateDir = path.resolve(url.fileURLToPath(import.meta.url), '../..', Object.values(Template).find((f) => f.name === template)!.template)

// copy template project to target
copy(templateDir, root)
await copyDirOrFile(templateDir, root)

skyclouds2001 marked this conversation as resolved.
Show resolved Hide resolved
// init git instance
const git = simpleGit({
Expand Down
2 changes: 1 addition & 1 deletion src/package.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* major package managers
* package managers enum
*/
export class PackageManager {
static readonly PNPM = 'pnpm'
Expand Down
Loading
Loading