Skip to content

Commit

Permalink
feat(cta): modify create-tensei-app to copy template from templates f…
Browse files Browse the repository at this point in the history
…older
  • Loading branch information
Frantz Kati committed Jan 26, 2021
1 parent 020a0aa commit 8f56edd
Show file tree
Hide file tree
Showing 21 changed files with 20,713 additions and 70 deletions.
35 changes: 35 additions & 0 deletions api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const { cms } = require('@tensei/cms')
const { media } = require('@tensei/media')
const { graphql } = require('@tensei/graphql')
const { tensei, resource, text, textarea, } = require('@tensei/core')

process.env.DEBUG = true

module.exports = tensei()
.resources([
resource('Tag')
.fields([
text('Name')
.creationRules('required')
.updateRules('min:25')
.searchable(),
textarea('Description')
.hideOnIndex(),
])
.displayField('Name'),
resource('Post')
.fields([
text('Title')
.rules('required', 'max:255', 'min:25')
.searchable()
.sortable()
])
]).plugins([
cms()
.plugin(),
media()
.graphql()
.plugin(),
graphql()
.plugin(),
])
1 change: 1 addition & 0 deletions packages/cli/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
9 changes: 9 additions & 0 deletions packages/cli/LICENCE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# The MIT License

Copyright 2020 Kati Frantz-Vallie, contributors

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.
1 change: 1 addition & 0 deletions packages/cli/kill-port.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'kill-port'
66 changes: 66 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"name": "@tensei/cli",
"version": "0.5.4",
"main": "./build/index.js",
"license": "MIT",
"types": "./build/index.d.ts",
"files": [
"build/",
"templates/"
],
"bin": {
"tensei": "./build/index.js"
},
"scripts": {
"prettier": "prettier --write './**/*.{js,json,ts,css}'",
"x-build": "tsc --p tsconfig.json",
"x-dev": "tsc --watch --p tsconfig.json",
"x-test": "jest --verbose --runInBand --forceExit"
},
"dependencies": {
"@types/change-case": "^2.3.1",
"@types/chokidar": "^2.1.3",
"@types/commander": "^2.12.2",
"@types/concurrently": "^5.2.1",
"@types/fs-extra": "^9.0.6",
"@types/listr": "^0.14.2",
"@types/node": "^14.14.10",
"@types/nodemon": "^1.19.0",
"@types/signale": "^1.4.1",
"@types/terminal-link": "^1.2.0",
"@types/yargs": "^15.0.12",
"chalk": "^4.1.0",
"change-case": "^4.1.1",
"check-node-version": "^4.0.3",
"chokidar": "^3.5.1",
"commander": "^6.2.0",
"concurrently": "^5.3.0",
"edge.js": "^1.1.4",
"execa": "^5.0.0",
"fs-extra": "^9.1.0",
"kill-port": "^1.6.1",
"latest-version": "^5.1.0",
"listr": "^0.14.3",
"nodemon": "^2.0.7",
"ora": "^5.1.0",
"signale": "^1.4.0",
"terminal-link": "^2.1.1",
"yargs": "^16.2.0",
"youch": "^2.1.1",
"youch-terminal": "^1.0.1"
},
"config": {
"commitizen": {
"path": "cz-conventional-changelog"
}
},
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
},
"publishConfig": {
"access": "public"
},
"gitHead": "e891d0776360c97f7d8035cb283b296739c29ff8"
}
15 changes: 15 additions & 0 deletions packages/cli/src/colors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import chalk from 'chalk'

export default {
error: chalk.bold.red,
warning: chalk.keyword('orange'),
success: chalk.greenBright,
info: chalk.grey,

header: chalk.bold.underline.hex('#e8e8e8'),
cmd: chalk.hex('#808080'),
tensei: chalk.hex('#56b3e2'),
love: chalk.redBright,

green: chalk.green
}
125 changes: 125 additions & 0 deletions packages/cli/src/commands/dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import Fs from 'fs'
import Path from 'path'
import Yargs from 'yargs'
import Youch from 'youch'
import { Server }from 'http'
import Nodemon from 'nodemon'
import Chokidar from 'chokidar'
import KillPort from 'kill-port'
import forTerminal from 'youch-terminal'
import terminalLink from 'terminal-link'
import { TenseiContract } from '@tensei/core'

import colors from '../colors'

export interface ArgsInterface {
forward: string
}

const port = (process.env.PORT || 8810) as number

let instance: Server|null = null

const WATCHER_IGNORE_EXTENSIONS = ['.db', '.sqlite', '-journal']

export const builder = (yargs: Yargs.Argv) => {
yargs.positional('', {
choices: ['api', 'web'],
default: ['api', 'web'],
description: `Which dev server(s) to start`,
type: '' as any
})
.epilogue(`
Also see the ${terminalLink(
'Tensei CLI Reference',
'https://tenseijs.com/reference/command-line-interface#dev'
)}
`)
}

const getApiFilePath = (file: string) => {
return Path.resolve(process.cwd(), file)
}

export const watchFilesWithChokidar = () => {

}

export const getFileName = () => {
let file: string = ''

// First, require the tensei app
if (Fs.existsSync(getApiFilePath('api.js'))) {
file = 'api.js'
} else if (Fs.existsSync(
getApiFilePath('server.js')
)) {
file = 'server.js'
} else if (
Fs.existsSync(
getApiFilePath('tensei.js')
)
) {
file = 'tensei.js'
} else {
console.error(
colors.error(`Could not find an API file. Please create either an api,server, or tensei.js file at your project root.`)
)
process.exit(1)
}

return file
}

export const getApiFile = () => {
return getApiFilePath(getFileName())
}

export const getTenseiInstance = () => {
const filePath = getApiFile()

Object.keys(require.cache).forEach((id) => {
if (id === filePath) {
delete require.cache[filePath]
}
})

return require(getApiFile())
}

export const errorHandler = async (error: Error) => forTerminal(await new Youch(error, null).toJSON())

export const reloadTenseiApp = async (tensei: TenseiContract = getTenseiInstance()) => {
// Third, run the migrate command on the tensei app
await tensei.migrate()

// .catch(errorHandler)
// Fourth, run the listen command on the tensei app
instance = await tensei.listen(port)

// .catch(errorHandler)
}

export const handler = async () => {
await reloadTenseiApp()

const watcher = Chokidar.watch(getFileName(), {
ignored: (file: string) => file.includes('node_modules') || WATCHER_IGNORE_EXTENSIONS.some((ext) => file.endsWith(ext)),
})

watcher.on('ready', () => {
watcher.on('all', async (event, path) => {
if (event === 'change') {
let startBuild = new Date().getTime()

process.stdout.write('Change detected, building... \n')

instance?.close(async () => {
await reloadTenseiApp()

console.log(`Done. Took ${new Date().getTime() - startBuild}ms.`)
})
}
})
})
}
13 changes: 13 additions & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env node

import yargs from 'yargs'

yargs
.commandDir('./commands')
.scriptName('tensei')
.example(
'yarn tensei build netlify',
"\"Build the tensei application for a netlify deployment'/'\""
)
.demandCommand()
.strict().argv
15 changes: 15 additions & 0 deletions packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "ES2017",
"module": "commonjs",
"declaration": true,
"outDir": "./build",
"strict": true,
"baseUrl": "./src",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"types": ["@types/jest", "node"]
},
"exclude": ["__tests__", "build"]
}
Loading

0 comments on commit 8f56edd

Please sign in to comment.