Skip to content

Commit

Permalink
Merge pull request #70 from sasjs/issue-67
Browse files Browse the repository at this point in the history
This closes #67 , closes #68 and closes #69
  • Loading branch information
krishna-acondy committed Sep 9, 2020
2 parents 167f37a + 52bc940 commit b930b0d
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 41 deletions.
86 changes: 52 additions & 34 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,9 @@ export async function cli(args) {
}
switch (command.name) {
case 'create':
const projectName = command.parameters[1]
const appType = processCreateParameters(command.parameters)
const { projectName, appType } = processCreateParameters(
command.parameters
)
await createFileStructure(projectName, appType)
break
case 'compile':
Expand Down Expand Up @@ -178,7 +179,7 @@ export async function cli(args) {
await add(command.parameters[1])
break
case 'run':
const { filePath, targetName } = command.parameters[1]
const { filePath, targetName } = command.parameters
await run(filePath, targetName)
break
default:
Expand Down Expand Up @@ -262,39 +263,56 @@ function processRunParameters(parameters) {
}
}

function processCreateParameters(parameters) {
const supportedTypes = ['react', 'angular', 'minimal']
let type = ''
if (
parameters.length > 2 &&
!(parameters[2] === '-t' || parameters[2] === '--template')
) {
console.error(
chalk.redBright(
`Invalid usage.\nCorrect syntax is ${chalk.cyanBright(
'sasjs create <app-name> -t <app-type>'
)} or ${chalk.cyanBright(
'sasjs create <app-name> --template <app-type>'
)}.`
)
function invalidSyntax() {
console.error(
chalk.redBright(
`Invalid usage.\nCorrect syntax is ${chalk.cyanBright(
'sasjs create <app-name> -t <app-type>'
)} or ${chalk.cyanBright(
'sasjs create <app-name> --template <app-type>'
)}.`
)
exit(1)
}
)
exit(1)
}

if (parameters.length !== 4) {
return type
}
type = parameters[3].trim()
if (!supportedTypes.includes(type)) {
console.error(
chalk.redBright(
`Invalid web app type.\nSupported types are ${chalk.cyanBright(
'angular'
)}, ${chalk.cyanBright('react')} and ${chalk.cyanBright('minimal')}.`
)
function invalidAppType() {
console.error(
chalk.redBright(
`Invalid web app type.\nSupported types are ${chalk.cyanBright(
'angular'
)}, ${chalk.cyanBright('react')}, ${chalk.cyanBright(
'minimal'
)} and ${chalk.cyanBright('sasonly')}.`
)
exit(1)
}
)
exit(1)
}

return type
function processCreateParameters(parameters) {
const supportedTypes = ['react', 'angular', 'minimal', 'sasonly']
let params = { projectName: undefined, appType: undefined }
switch (parameters.length) {
case 1: // sasjs create
break
case 2: // sasjs create <app_name>
params.projectName = parameters[1]
break
case 3: // sasjs create -t <type>
if (parameters[1] === '-t' || parameters[1] === '--template')
params.appType = parameters[2].trim()
else invalidSyntax()
if (!supportedTypes.includes(params.appType)) invalidAppType()
break
case 4: // sasjs create <app_name> -t <type>
if (parameters[2] === '-t' || parameters[2] === '--template') {
params.projectName = parameters[1]
params.appType = parameters[3].trim()
} else invalidSyntax()
if (!supportedTypes.includes(params.appType)) invalidAppType()
break
default:
invalidSyntax()
}
return params
}
40 changes: 40 additions & 0 deletions src/config-sasonly.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"folders": [
{
"folderName": "sasjs",
"files": [],
"subFolders": [
{
"folderName": "db"
},
{
"folderName": "build"
},
{
"folderName": "macros"
},
{
"folderName": "services",
"files": [],
"subFolders": [
{
"folderName": "common",
"files": [
{
"fileName": "appinit.sas",
"content": "/**\n @file appinit.sas\n @brief Initialisation service - runs on app startup\n @details This is always the first service called when the app is opened.\n\n <h4> Dependencies </h4>\n\n**/\n\nproc sql;\ncreate table areas as select distinct area\n from sashelp.springs;\n%webout(OPEN)\n%webout(OBJ,areas)\n%webout(CLOSE)\n"
}
]
}
]
}
]
}
],
"config": {
"cmnMacros": ["macros"],
"cmnServices": ["services/common"],
"useMacroCore": true,
"targets": []
}
}
2 changes: 2 additions & 0 deletions src/sasjs-add/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { create } from '../sasjs-create'
import { getUserInput } from '../utils/input-utils'
import chalk from 'chalk'
import path from 'path'
Expand Down Expand Up @@ -69,6 +70,7 @@ async function getLocalConfig() {
const config = await getConfiguration(
path.join(buildSourceFolder, 'sasjsconfig.json')
)
if (!config) await create('.', 'sasonly')
return config
}

Expand Down
12 changes: 7 additions & 5 deletions src/sasjs-create/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import {
import chalk from 'chalk'

export async function create(parentFolderName = '.', appType = '') {
const config = await getConfiguration(path.join(__dirname, '../config.json'))
const fileStructure = await getFileStructure()
const configPath =
appType === 'sasonly' ? '../config-sasonly.json' : '../config.json'
const config = await getConfiguration(path.join(__dirname, configPath))
const fileStructure = await getFileStructure(appType === 'sasonly')
console.log(chalk.greenBright('Creating folders and files...'))
if (parentFolderName !== '.') {
await createFolder(path.join(process.projectDir, parentFolderName))
Expand Down Expand Up @@ -58,12 +60,12 @@ export async function create(parentFolderName = '.', appType = '') {
})
}

if (!appType) {
if (!appType || appType === 'sasonly') {
await setupNpmProject(parentFolderName)
}
await setupGitIgnore(parentFolderName)
}

async function getFileStructure() {
return await getFolders()
async function getFileStructure(sasOnly = false) {
return await getFolders(sasOnly)
}
5 changes: 3 additions & 2 deletions src/utils/config-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ export async function saveLocalRcFile(content) {
await createFile(path.join(projectRoot, '.sasjsrc'), content)
}

export async function getFolders() {
const config = await readFile(path.join(__dirname, '../config.json'))
export async function getFolders(sasOnly = false) {
const configPath = sasOnly ? '../config-sasonly.json' : '../config.json'
const config = await readFile(path.join(__dirname, configPath))
if (config) {
const configJson = JSON.parse(config)
return Promise.resolve(configJson.folders)
Expand Down

0 comments on commit b930b0d

Please sign in to comment.