Merge pull request #52 from datopian/feature/create-portal-app

Feature/create portal app
This commit is contained in:
Rufus Pollock 2020-12-02 16:58:33 +01:00 committed by GitHub
commit 8e4c153d21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 16586 additions and 1 deletions

View File

@ -3,5 +3,8 @@
"private": true,
"devDependencies": {
"lerna": "^3.22.1"
}
},
"workspaces":[
"packages/**"
]
}

1
packages/create-portal-app/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules

View File

@ -0,0 +1,7 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": false,
"singleQuote": true,
"printWidth": 79
}

View File

@ -0,0 +1,36 @@
# Create Portal App
<br/>
<img src="resources/create-portal-app.gif">
<br/>
<br/>
This is a friendly way to start using `portal.js`. Using this CLI helps you to set up fast and easly one portal.js with the default template.
The CLI automatically create the project folder, install main dependencies, generate a new git repository and change package name.
<br/>
## Requirements
- Yarn (> 1.22.10) or Npm (> 6.14.5)
- Git
<br/>
## How to use
To create a new portal.js application you can send the project name as first argument.
```
create-portal-app my-app
```
<br/>
## Options
`create-portal-app` comes with options:
- **--use-npm** - This option allows to use npm instead yarn as default package manager

View File

@ -0,0 +1,23 @@
const path = require('path')
const fs = require('fs')
const spawn = require('cross-spawn')
function parserPath(projectPath) {
return [process.cwd(), projectPath].join(path.sep)
}
function copy(root, destination) {
const destinationPath = parserPath(destination)
return spawn.sync('cp', ['-r', root, destinationPath])
}
function isPathInUse(projectPath) {
const fullPath = parserPath(projectPath)
const isPathExists = fs.existsSync(fullPath)
if (isPathExists) {
return fs.readdirSync(fullPath).length
}
return isPathExists
}
module.exports = { parserPath, copy, isPathInUse }

View File

@ -0,0 +1,52 @@
const spawn = require('cross-spawn')
const path = require('path')
const execSync = require('child_process').execSync
const semver = require('semver')
/**
*
* @param {String} projectName Project name to be created
* @param {Boolean} isYarn Check if will be installed under yarn or NPM
*/
function install(projectName, isYarn) {
return new Promise((resolve, reject) => {
const appPath = [process.cwd(), projectName].join(path.sep)
//change the directory to the app directory
process.chdir(appPath)
const command = isYarn ? 'yarn' : 'npm'
const args = isYarn ? [''] : ['install']
const exec = spawn(command, args, {
stdio: 'pipe',
env: { ...process.env, ADBLOCK: '1', DISABLE_OPENCOLLECTIVE: '1' },
})
exec.on('close', (code) => {
if (code !== 0) {
reject({ command: `${command} ${args[0]}` })
return
}
resolve()
})
})
}
/**
* Method to initialize git repo on the new project
*/
async function initGit() {
spawn(`git`, [`init`, `-q`])
}
/**
* Check the version for npm and Yarn
* @param {String} pname
* @returns Boolean
*/
function checkPackageVersion(pname) {
let userVersion = execSync(`${pname} --version`).toString()
let expectedVersion = pname === 'yarn' ? '1.22.10' : '6.14.5'
return !semver.lt(userVersion, expectedVersion)
}
module.exports = { install, initGit, checkPackageVersion }

View File

@ -0,0 +1,18 @@
const fs = require('fs')
const { parserPath } = require('./copy')
function _replacePackage(projectName) {
const appPath = parserPath(projectName)
const packagePath = `${appPath}/package.json`
const data = JSON.parse(fs.readFileSync(packagePath, 'utf8'))
data.name = projectName
delete data.homepage
fs.writeFileSync(packagePath, JSON.stringify(data, null, 2))
}
function replaceContent(projectName) {
_replacePackage(projectName)
}
module.exports = replaceContent

View File

@ -0,0 +1,163 @@
#!/usr/bin/env node
const Listr = require('listr')
const { program } = require('commander')
const chalk = require('chalk')
const prompts = require('prompts')
const path = require('path')
const figlet = require('figlet')
const package = require('./package.json')
const { copy, isPathInUse } = require('./helpers/copy')
const { install, initGit, checkPackageVersion } = require('./helpers/install')
const replace = require('./helpers/replace')
// Output path to create new portal app
let projectPath = ''
// Commander parameters to specify CLI behavior
program
.name(package.name)
.version(package.version)
.arguments('[dir]')
.usage(`${chalk.yellow('[dir]')}`)
.description({
dir: 'Directory to be used on install Portal.js',
})
.action((name) => (projectPath = name))
.option('--use-npm', 'Install dependencies using npm instead yarn')
.parse(process.argv)
/**
* Method to ask a custon name if was not passed as parameter
* returns the value passed from terminal input
*/
async function promptPath() {
return prompts({
type: 'text',
name: 'path',
message: 'Choose a name to your project',
initial: '',
validate: (name) => {
projectPath = name
if (isPathInUse(projectPath)) {
return `${chalk.yellow(
'Path ' +
chalk.redBright(projectPath) +
' is already in use and is not empty.'
)}`
}
return true
},
})
}
/**
* Main method to start CLI and validate inputs
*/
async function run() {
if(checkPackageVersion(program.useNpm? 'npm' : 'yarn')) {
console.log()
console.log(`${chalk.yellowBright('Your package manager version is outdated, please update before continue.')}`)
console.log()
return
}
if (typeof projectPath === 'string') {
projectPath = projectPath.trim()
}
if (!projectPath) {
const response = await promptPath()
if (typeof response.path === 'string') {
projectPath = response.path.trim()
}
}
if (!projectPath) {
//TODO separate log methods
console.log()
console.log('Please choose a name to your project:')
console.log()
console.log('Example:')
console.log(
`${chalk.cyan(program.name())} ${chalk.yellow('ny-portal-app')}`
)
console.log()
process.exit(1)
}
const root = path.join(__dirname + '/../portal')
if (isPathInUse(projectPath)) {
console.log()
console.log(
`${chalk.yellow(
'Path ' +
chalk.redBright(projectPath) +
' is already in use and is not empty.'
)}`
)
console.log()
process.exit(1)
}
// print a fancy Portal.js in the terminal
console.log(
chalk.yellow(figlet.textSync('Portal.Js', { horizontalLayout: 'full' }))
)
console.log()
console.log(`Creating new portal.js app in ${chalk.cyan(projectPath)}`)
console.log()
//Tasks workflow
const tasks = new Listr([
{
title: 'Fetching Content',
task: () => copy(root, projectPath),
},
{
title: 'Updating Content',
task: () => replace(projectPath),
},
{
title: 'Installing Dependencies',
task: () => install(projectPath, program.useNpm),
},
{
title: 'Git Init',
task: () => initGit(projectPath),
},
])
tasks.run().then(() => {
console.log()
console.log(`${chalk.greenBright('Instalation Completed Successfully')}`)
console.log()
console.log(
`Run ${chalk.cyan('cd ' + projectPath)} and ${chalk.green(program.useNpm ? 'npm run dev' : 'yarn dev')}`
)
console.log()
console.log('Enjoy =)')
})
}
//Main CLI execution workflow
run().catch((error) => {
console.log(error.name)
if (error.install) {
console.log()
console.log(
`${chalk.redBright('Error on Create App :')}${chalk.yellow(
error.message.toString()
)}`
)
} else {
console.log(`${chalk.red('Unexpected Error. Please report it as a bug')}`)
console.log(error)
}
console.log()
process.exit(1)
})

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,27 @@
{
"name": "create-portal-app",
"version": "1.0.0",
"description": "Create Portal App CLI",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"chalk": "^4.1.0",
"commander": "^6.2.0",
"cpy": "^8.1.1",
"cross-spawn": "^7.0.3",
"figlet": "^1.5.0",
"listr": "^0.14.3",
"ora": "^5.1.0",
"prompts": "^2.4.0"
},
"files": [
"bin"
],
"bin": {
"create-portal-app": "index.js"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

View File

@ -0,0 +1,14 @@
{
"name": "Datahub",
"version": "1.0.0",
"description": "Create Portal App CLI",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"chalk": "^4.1.0"
}
}

2
packages/portal/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
node_modules/
.next/

14268
yarn.lock Normal file

File diff suppressed because it is too large Load Diff