{"size": 2424, "ext": "js", "max_stars_repo_path": "bin/lint.js", "max_stars_repo_name": "thesold/stacklint", "max_stars_repo_stars_event_min_datetime": "2017-12-02T20:46:46.000Z", "max_stars_repo_stars_event_max_datetime": "2021-04-12T01:53:58.000Z", "max_issues_repo_path": "bin/lint.js", "max_issues_repo_name": "thesold/stacklint", "max_issues_repo_issues_event_min_datetime": "2017-12-02T20:45:12.000Z", "max_issues_repo_issues_event_max_datetime": "2018-07-30T07:11:32.000Z", "max_forks_repo_path": "bin/lint.js", "max_forks_repo_name": "thesold/stacklint", "max_forks_repo_forks_event_min_datetime": "2021-01-17T13:47:39.000Z", "max_forks_repo_forks_event_max_datetime": "2021-01-17T13:47:39.000Z"}
{"max_stars_count": 12, "max_issues_count": 9, "max_forks_count": 1, "avg_line_length": 28.1860465116, "max_line_length": 96, "alphanum_fraction": 0.6278877888}
#! /usr/bin/env node

const fs = require('fs')
const path = require('path')
const mergeJson = require('../src/helpers/mergejson')
const argv = require('minimist')(process.argv.slice(2))
const inquirer = require('inquirer')

const { COPYFILE_EXCL } = fs.constants

const stacks = argv._

if (argv.l || argv.list) {
    fs.readdir(path.resolve(__dirname, '../src/stacks/'), (err, stackFolders) => {
        stackFolders.forEach((stack) => {
            if (stack !== 'all') console.log(stack)
        })
    })

    return
}

if (!stacks.length) {
    console.warn('Please specify at least one stack to install')
    return
}

let stackFileList = []

stacks.forEach((stack) => {
    const stackPathJson = path.resolve(__dirname, '../src/stacks/', stack, 'json')
    const stackPathStubs = path.resolve(__dirname, '../src/stacks/', stack, 'stubs')

    if (fs.existsSync(stackPathStubs)) {
        stackFileList = stackFileList.concat(fs.readdirSync(stackPathStubs))
    }

    if (fs.existsSync(stackPathJson)) {
        stackFileList = stackFileList.concat(fs.readdirSync(stackPathJson))
    }
})

stackFileList = Array.from(new Set(stackFileList)).sort().join('\n')

inquirer.prompt([{
    type: 'confirm',
    name: 'install',
    message: `
The following files will be created/updated.
${stackFileList}
Are you sure you want to continue?
`,
    default: true,
}]).then((answer) => {
    if (!answer.install) {
        console.log('No stacks were installed')
        return false
    }

    ['all'].concat(stacks).forEach((stack) => {
        const stackPathJson = path.resolve(__dirname, '../src/stacks/', stack, 'json')
        const stackPathStubs = path.resolve(__dirname, '../src/stacks/', stack, 'stubs')

        if (fs.existsSync(stackPathStubs)) {
            const stubFiles = fs.readdirSync(stackPathStubs)

            stubFiles.forEach((fileName) => {
                if (fs.existsSync(fileName)) return

                fs.copyFileSync(path.resolve(stackPathStubs, fileName), fileName, COPYFILE_EXCL)
            })
        }

        if (fs.existsSync(stackPathJson)) {
            const stackFiles = fs.readdirSync(stackPathJson)

            stackFiles.forEach((fileName) => {
                mergeJson.files(path.resolve(stackPathJson, fileName), fileName)
            })
        }
    })

    console.log('Stack linting is now installed. Please run npm/composer install as required.')
    return true
})
