lina-infratech-cli
Version:
A CLI tool for Lina Infratech Developers
202 lines (163 loc) • 4.24 kB
text/typescript
import axios from 'axios'
import { execSync } from 'child_process'
import { logSuccess, logWarning } from '../utils/chalk/colors'
import { MESSAGES } from '../ui/messages'
import { writeFile } from '../utils/writer'
function generatePrettierConfig() {
const fileName = '.prettierrc'
const content = `{
'trailingComma': 'es5',
'semi': false,
'singleQuote': true,
'bracketSpacing': true,
'tabWidth': 2,
'useTabs': true,
'printWidth': 80,
'endOfLine': 'lf',
}`
writeFile(fileName, content)
}
function generateTsConfig() {
const fileName = 'tsconfig.json'
const content = `{
"compilerOptions": {
"incremental": true,
"target": "es2016",
"experimentalDecorators": true,
"module": "commonjs",
"allowJs": true,
"checkJs": true,
"declaration": true,
"outDir": "./dist",
"removeComments": true,
"importHelpers": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"skipLibCheck": true
}
}`
writeFile(fileName, content)
}
function generateNodemonJson() {
const fileName = 'nodemon.json'
const content = {
'ignore': ['node_modules/**', 'dist/**'],
}
writeFile(fileName, JSON.stringify(content))
}
function generateLinterFile() {
const fileName = '.eslintrc.js'
const content = `module.exports = {
env: {
es6: true,
node: true,
},
extends: ['airbnb-base', 'prettier'],
plugins: ['prettier'],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
},
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
},
rules: {
'no-new': 0,
'no-template-curly-in-string': 0,
'import/no-named-as-default': 0,
'prettier/prettier': 'error',
'class-methods-use-this': 'off',
'no-param-reassign': 'off',
camelcase: 'off',
'no-unused-vars': ['error', { argsIgnorePattern: 'next' }],
ident: ['error', 'tab'],
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
rules: {
'prettier/prettier': 0,
},
root: true,
env: {
node: true,
jest: true,
},
}`
writeFile(fileName, content)
}
async function generateGitFiles() {
execSync('git init')
const fileName = '.gitignore'
let { data } = await axios(
'https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore',
)
data += `*.swp\n.vscode\n.DS_Store`
writeFile(fileName, data)
}
function generateDockerIgnore() {
const fileName = '.dockerignore'
const content = '.git\nnode_modules\ndist\n.env*\ndevops'
writeFile(fileName, content)
}
function generateDockerFiles() {
generateDockerIgnore()
const fileName = 'Dockerfile'
const content = `###################
# BUILD FOR LOCAL DEVELOPMENT
###################
FROM node:18-alpine As development
WORKDIR /usr/src/app
COPY --chown=node:node package*.json ./
RUN npm ci
COPY --chown=node:node . .
USER node
###################
# BUILD FOR PRODUCTION
###################
FROM node:18-alpine As build
WORKDIR /usr/src/app
COPY --chown=node:node package*.json ./
COPY --chown=node:node --from=development /usr/src/app/node_modules ./node_modules
COPY --chown=node:node . .
RUN npm run build
ENV NODE_ENV prod
RUN npm ci --only=production && npm cache clean --force
USER node
###################
# PRODUCTION
###################
FROM node:18-alpine As production
COPY --chown=node:node --from=build /usr/src/app/node_modules ./node_modules
COPY --chown=node:node --from=build /usr/src/app/dist ./dist
CMD [ "node", "dist/src/server.js" ]`
writeFile(fileName, content)
logWarning('You should change node:lts-alpine image to fixed NodeJS version.')
}
export async function generateConfigFiles() {
console.log(
MESSAGES.GENERATE_CONFIG_FILES(
'prettier',
'typescript',
'docker',
'eslint',
'nodemon',
),
)
generatePrettierConfig()
generateTsConfig()
generateLinterFile()
generateNodemonJson()
generateDockerFiles()
await generateGitFiles()
logSuccess(MESSAGES.CONFIG_FILES_OK)
}