
import {ls, echo} from 'shelljs'
import exec from '../util/exec'
import yaml from 'js-yaml'

/**
 * Creator for gh-pages
 */
export default class GhPagesCreator {


    /**
     * @public
     */
    create(dir ?: string,
           remote ?: string = 'origin') {

        this.exec('git checkout --orphan gh-pages')

        if (dir) {

            this.exec('git reset')
            this.exec(`git add -f ${dir}`)
            this.exec('git clean -fdx')

            ls(dir).forEach(file => {
                this.exec(`git mv ${dir}/${file} .`)
            })
        }
        this.addCircleYml()

        this.exec('git commit -m "gh-pages"')
        this.exec(`git push --force ${remote} gh-pages`)
    }

    /**
     * Add circle.yml for gh-pages
     * @private
     */
    addCircleYml() {
        const ignoreGhPagesYml = {
            general: {branches: {ignore: ['gh-pages']}}
        }
        const ymlStr = yaml.dump(ignoreGhPagesYml, {indent: 2, lineWidth: 120})
        this.write('circle.yml', ymlStr)
        this.exec('git add -f circle.yml')
    }

    /**
     * Write a file with the content
     * @private
     */
    write(filename: string, content: string) {
        echo(content).to(filename)
    }

    /**
     * execute a given command
     * @private
     */
    exec(...args: Array<any>): Object {
        return exec(...args)
    }

}

@@ -5,17 +5,31 @@
 ort 
-%7Bls, echo
+fs from 'fs'%0Aimport %7Bls
 %7D fr
@@ -1208,33 +1208,64 @@
     
-echo(content).to(filename
+fs.writeFileSync(process.cwd() + '/' + filename, content
 )%0A  

change implementation of writing circle.yml for gh-pages