Skip to content

Commit

Permalink
add templates & template copying
Browse files Browse the repository at this point in the history
  • Loading branch information
bdbch committed Feb 26, 2023
1 parent 1d0f7b5 commit 0700328
Show file tree
Hide file tree
Showing 19 changed files with 311 additions and 21 deletions.
58 changes: 58 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"templates/*"
],
"devDependencies": {
"@types/fs-extra": "^11.0.1",
"@types/minimist": "^1.2.2",
"@types/node": "^18.14.1",
"@types/prompts": "^2.4.2",
Expand All @@ -36,6 +37,7 @@
},
"dependencies": {
"chalk": "^5.2.0",
"fs-extra": "^11.1.0",
"minimist": "^1.2.8",
"prompts": "^2.4.2"
}
Expand Down
28 changes: 17 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import chalk from 'chalk'
import minimist from 'minimist'
import prompts from 'prompts'
import { defaultDir, defaultName, defaultPackage } from './constants.js'
import { copyProjectTemplate } from './lib/copyProjectTemplate.js'
import { createProjectDir } from './lib/createProjectDir.js'
import { updatePackageJson } from './lib/updatePackageJson.js'
import { updateReadme } from './lib/updateReadme.js'

async function init() {
const argv = minimist(process.argv.slice(2))
Expand Down Expand Up @@ -49,8 +52,8 @@ async function init() {
name: 'projectTemplate',
message: 'Project template',
choices: [
{ title: 'Extension without TypeScript', value: 'js' },
{ title: 'Extension with TypeScript', value: 'ts' },
{ title: 'Extension without TypeScript', value: 'extension-js' },
{ title: 'Extension with TypeScript', value: 'extension-ts' },
],
initial: 0,
onState: (state: any) => {
Expand All @@ -61,18 +64,21 @@ async function init() {

const { projectDir, projectName, projectPackage, projectTemplate } = result

console.log(chalk.green('Creating project...'))
console.log('\n\nCreating project directory...')

const relativeDirPath = createProjectDir(projectDir)
const relativeDirPath = await createProjectDir(projectDir)

if (!relativeDirPath) {
console.log(chalk.red('Error creating project directory. Aborting.'))
return
}
console.log(chalk.green('Created project directory.'))

// copyProjectTemplate(relativeDirPath, projectTemplate)
// updatePackageJson(projectDir, projectName, projectPackage)
// updateReadme(projectDir, projectName)
console.log('\n\nCopying project template...')

await copyProjectTemplate(relativeDirPath, projectTemplate)

console.log(chalk.green('Copied project template.'))

updatePackageJson(relativeDirPath, projectName, projectPackage)

updateReadme(relativeDirPath, projectName, projectPackage)
} catch (e: any) {
console.log(e)
return
Expand Down
25 changes: 25 additions & 0 deletions src/lib/copyProjectTemplate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import path from 'path'
import fs from 'fs'
import fsExtra from 'fs-extra'
import { fileURLToPath } from 'url';

const { copySync } = fsExtra

const templatesDir = path.resolve(
fileURLToPath(import.meta.url),
"../../../templates"
);

export const copyProjectTemplate = async (projectPath: string, template: string) => {
const templatePath = `${templatesDir}/${template}`;
const templateExists = fs.existsSync(templatePath)

if (!templateExists) {
console.error(`Template ${template} does not exist`)
process.exit(1)
}

copySync(templatePath, projectPath, { overwrite: true })

return projectPath
}
16 changes: 10 additions & 6 deletions src/lib/createProjectDir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,23 @@ export const createProjectDir = async (dir: string) => {

if (!overwrite) {
console.log(chalk.red('Directory already exists. Aborting.'))
return false
process.exit(1)
}

// if the user wants to overwrite the directory, delete it
await fs.rmdir(relativePath, { recursive: true }, (err) => {
if (err) {
throw err
}
await new Promise((resolve) => {
fs.rm(relativePath, { recursive: true }, (err) => {
if (err) {
throw err
}

resolve(true)
})
})
}

// create the directory
await fs.mkdir(relativePath, { recursive: true }, (err) => {
await fs.mkdir(relativePath, { recursive: true }, err => {
if (err) {
throw err
}
Expand Down
15 changes: 15 additions & 0 deletions src/lib/updatePackageJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import fs from 'fs'
import path from 'path'

export const updatePackageJson = async (projectPath: string, projectName: string, projectPackage: string) => {
const packageJsonPath = `${projectPath}/package.json`
const jsonContent = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))

jsonContent.name = projectPackage

const packageJsonString = JSON.stringify(jsonContent, null, 2)

await fs.writeFileSync(packageJsonPath, packageJsonString)

return projectPath
}
13 changes: 13 additions & 0 deletions src/lib/updateReadme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import fs from "fs"
import path from "path"

export const updateReadme = async (relativeDirPath: string, projectName: string, projectPackage: string) => {
const readmePath = path.resolve(process.cwd(), `${relativeDirPath}/README.md`)
const readmeContent = fs.readFileSync(readmePath, 'utf8')

const newReadmeContent = readmeContent.replace(/{{NAME}}/g, projectName).replace(/{{PKG}}/g, projectPackage)

fs.writeFileSync(readmePath, newReadmeContent)

return relativeDirPath
}
2 changes: 2 additions & 0 deletions templates/extension-js/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
9 changes: 9 additions & 0 deletions templates/extension-js/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# {{NAME}}

A fresh Tiptap extension.

## Installation

```bash
npm install {{PKG}}
```
24 changes: 22 additions & 2 deletions templates/extension-js/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
{
"name": "<%= NAME %>",
"name": "",
"version": "1.0.0",
"description": ""
"description": "",
"files": [
"dist"
],
"main": "dist/index.cjs.js",
"module": "dist/index.js",
"scripts": {
"build": "rollup -c",
"develop": "rollup -c -w"
},
"devDependencies": {
"@rollup/plugin-babel": "^6.0.3",
"@rollup/plugin-commonjs": "^24.0.1",
"@tiptap/core": "^2.0.0-beta.218",
"rollup": "^3.17.3",
"rollup-plugin-auto-external": "^2.0.0",
"rollup-plugin-sourcemaps": "^0.6.3"
},
"peerDependencies": {
"@tiptap/core": "^2.0.0-beta.218"
}
}
32 changes: 32 additions & 0 deletions templates/extension-js/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// rollup.config.js

const autoExternal = require("rollup-plugin-auto-external");
const sourcemaps = require("rollup-plugin-sourcemaps");
const commonjs = require("@rollup/plugin-commonjs");
const babel = require("@rollup/plugin-babel");

const config = {
input: "src/index.js",
output: [
{
file: "dist/index.cjs.js",
format: "cjs",
exports: "named",
sourcemap: true,
},
{
file: "dist/index.js",
format: "esm",
exports: "named",
sourcemap: true,
},
],
plugins: [
autoExternal({ packagePath: "./package.json" }),
sourcemaps(),
babel(),
commonjs(),
],
};

module.exports = config;
11 changes: 11 additions & 0 deletions templates/extension-js/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Extension } from '@tiptap/core'

const MyExtension = Extension.create({
name: "MyExtension",

// do your stuff here
})

export { MyExtension }

export default MyExtension
2 changes: 2 additions & 0 deletions templates/extension-ts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
9 changes: 9 additions & 0 deletions templates/extension-ts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# {{NAME}}

A fresh Tiptap extension.

## Installation

```bash
npm install {{PKG}}
```
24 changes: 22 additions & 2 deletions templates/extension-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
{
"name": "<%= NAME %>",
"name": "",
"version": "1.0.0",
"description": ""
"description": "",
"main": "dist/index.cjs.js",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "rollup -c",
"develop": "rollup -c -w"
},
"devDependencies": {
"@rollup/plugin-babel": "^6.0.3",
"@rollup/plugin-commonjs": "^24.0.1",
"@tiptap/core": "^2.0.0-beta.218",
"rollup": "^3.17.3",
"rollup-plugin-auto-external": "^2.0.0",
"rollup-plugin-sourcemaps": "^0.6.3",
"rollup-plugin-typescript2": "^0.34.1",
"typescript": "^4.9.5"
},
"peerDependencies": {
"@tiptap/core": "^2.0.0-beta.218"
}
}
Loading

0 comments on commit 0700328

Please sign in to comment.