diff --git a/src/cli/src/config.ts b/src/cli/src/config.ts index c9ded2c..8ab4657 100644 --- a/src/cli/src/config.ts +++ b/src/cli/src/config.ts @@ -8,6 +8,7 @@ const reserved = { const root = path.join(__dirname, '..', '..') const cwd = process.cwd() +export const dotElmSpa = path.join(cwd, '.elm-spa'); const config = { reserved, @@ -16,18 +17,18 @@ const config = { src: path.join(cwd, 'src'), pages: { src: path.join(cwd, 'src', 'Pages'), - defaults: path.join(cwd, '.elm-spa', 'defaults', 'Pages') + defaults: path.join(dotElmSpa, 'defaults', 'Pages') }, defaults: { src: path.join(root, 'src', 'defaults'), - dest: path.join(cwd, '.elm-spa', 'defaults') + dest: path.join(dotElmSpa, 'defaults') }, - generated: path.join(cwd, '.elm-spa', 'generated'), + generated: path.join(dotElmSpa, 'generated'), templates: { defaults: path.join(root, 'src', 'templates', 'add'), - user: path.join(cwd, '.elm-spa', 'templates') + user: path.join(dotElmSpa, 'templates') }, - package: path.join(cwd, '.elm-spa', 'package'), + package: path.join(dotElmSpa, 'package'), public: path.join(cwd, 'public'), dist: path.join(cwd, 'public', 'dist'), }, diff --git a/src/cli/src/index.ts b/src/cli/src/index.ts index 3f3887c..4ba7cee 100644 --- a/src/cli/src/index.ts +++ b/src/cli/src/index.ts @@ -1,9 +1,11 @@ #!/usr/bin/env node import CLI from './cli' +import { dotElmSpa } from './config' +import { checkIfFolderExists } from './file' import { Commands } from './types' -const commands : Commands = { +const commands: Commands = { new: CLI.new, add: CLI.add, build: CLI.build, @@ -16,13 +18,23 @@ const commands : Commands = { make: CLI.build, } -const command : string | undefined = process.argv[2] +const commandsNotRequiringInitialization = ['new', 'init', 'help'] + +const command: string | undefined = process.argv[2] Promise.resolve(command) + .then(async cmd => { + if (!commandsNotRequiringInitialization.includes(cmd as string)) { + if (!await checkIfFolderExists(dotElmSpa)) { + throw ' This command must be run in a project folder containing a .elm-spa folder.' + } + } + return cmd + }) .then(cmd => commands[cmd as keyof Commands] || commands.help) .then(task => task()) .then(output => { - const message = output instanceof Array ? output : [ output ] + const message = output instanceof Array ? output : [output] console.info('') console.info(message.join('\n\n')) })