<commit_before>'use babel';

import fs from 'fs'
import path from 'path'
import { CompositeDisposable } from 'atom'
import { exec, spawn } from 'child_process'

const RIOT_PATH = process.env.RIOT_PATH || '/usr/local/bin/riot'

let subscriptions;

export let config = {
    compileOnSave: {
        title: 'Compile on Save',
        description: 'Compile on file save.',
        type: 'boolean',
        default: true,
    },
}

export function activate() {
    subscriptions = new CompositeDisposable(
        atom.commands.add('atom-text-editor', 'core:save', (event) => onCommandCompileOnSave(event.currentTarget.getModel())),
        atom.commands.add('atom-workspace', 'riot:compile', () => onCommandCompile(atom.workspace.getActiveTextEditor()))
    );
}

export function deactivate() {
    subscriptions.dispose()
}

function onCommandCompileOnSave(textEditor) {
    if (atom.config.get('riot.compileOnSave') === false) return

    onCommandCompile(textEditor)
}

function onCommandCompile(textEditor) {
    if (!textEditor) return

    let riotGrammar = atom.grammars.grammarForScopeName('source.riot');

    if (textEditor.getGrammar() == riotGrammar) {
        compileRiot(textEditor.getPath())
    }
}

function endsWith(string, suffix) {
    var sub = string.length - suffix.length;
    return (sub >= 0) && (string.lastIndexOf(suffix) === sub)
}

function compileRiot(sourcePath) {
    console.log('compile', sourcePath)

    let ext = (filePath) => {
        for (let ext of ['riot.tag', 'riot.html', 'tag', 'html']) {
            if (endsWith(filePath, '.' + ext)) {
                return '.' + ext
            }
        }
        return path.extname(filePath)
    }(sourcePath)

    let command = '"' + riotCommandPath() + '" "' + sourcePath + '"' + ' --ext=' + ext.slice(1)

    exec(command, { env: process.env }, function(error, stdout, stderr) {
        if (error) {
            console.error(error)

            atom.notifications.addError(
                'Riot compile error',
                {
                    dismissable: true,
                    detail: stdout || stderr,
                }
            )

            return
        }

        console.log(stdout)

        let destPath = sourcePath.substring(0, sourcePath.length - ext.length) + '.js'
        atom.notifications.addSuccess(`Saved to ${destPath}`);
    })
}

function riotCommandPath() {
    // 1. global command
    if (fs.existsSync(RIOT_PATH)) {
        return RIOT_PATH
    }

    // 2. package local command
    let atomPackage = atom.packages.getLoadedPackage('riot')

    if (!atomPackage) {
        throw new Error('package broken')
    }

    return atomPackage.path + '/node_modules/.bin/riot'
}<commit_msg>Support package setting for global riot-cli path.<commit_after>'use babel';

import fs from 'fs'
import path from 'path'
import { CompositeDisposable } from 'atom'
import { exec, spawn } from 'child_process'

let subscriptions

export let config = {
    compileOnSave: {
        title: 'Compile on save',
        description: 'Compile on file save.',
        type: 'boolean',
        default: true,
    },

    riotPath: {
        title: 'Riot-CLI path',
        description: 'Path for Global riot command.',
        type: 'string',
        default: process.env.RIOT_PATH || '/usr/local/bin/riot',
    },
}

export function activate() {
    subscriptions = new CompositeDisposable(
        atom.commands.add('atom-text-editor', 'core:save', (event) => onCommandCompileOnSave(event.currentTarget.getModel())),
        atom.commands.add('atom-workspace', 'riot:compile', () => onCommandCompile(atom.workspace.getActiveTextEditor()))
    )
}

export function deactivate() {
    subscriptions.dispose()
}

function onCommandCompileOnSave(textEditor) {
    if (atom.config.get('riot.compileOnSave') === false) return

    onCommandCompile(textEditor)
}

function onCommandCompile(textEditor) {
    if (!textEditor) return

    let riotGrammar = atom.grammars.grammarForScopeName('source.riot')

    if (textEditor.getGrammar() == riotGrammar) {
        compileRiot(textEditor.getPath())
    }
}

function endsWith(string, suffix) {
    var sub = string.length - suffix.length
    return (sub >= 0) && (string.lastIndexOf(suffix) === sub)
}

function compileRiot(sourcePath) {
    console.log('compile', sourcePath)

    let ext = (filePath) => {
        for (let ext of ['riot.tag', 'riot.html', 'tag', 'html']) {
            if (endsWith(filePath, '.' + ext)) {
                return '.' + ext
            }
        }
        return path.extname(filePath)
    }(sourcePath)

    let command = '"' + riotCommandPath() + '" "' + sourcePath + '"' + ' --ext=' + ext.slice(1)

    exec(command, { env: process.env }, function(error, stdout, stderr) {
        if (error) {
            console.error(error)

            atom.notifications.addError(
                'Riot compile error',
                {
                    dismissable: true,
                    detail: stdout || stderr,
                }
            )

            return
        }

        console.info(stdout)

        let destPath = sourcePath.substring(0, sourcePath.length - ext.length) + '.js'
        atom.notifications.addSuccess(`Saved to ${destPath}`)
    })
}

function riotCommandPath() {
    // 1. global command
    let globalRiotPath = atom.config.get('riot.riotPath')
    if (fs.existsSync(globalRiotPath)) {
        return globalRiotPath
    }
    console.warn('Global riot command not found', globalRiotPath);

    // 2. package local command
    let atomPackage = atom.packages.getLoadedPackage('riot')

    if (!atomPackage) {
        throw new Error('package broken')
    }

    return atomPackage.path + '/node_modules/.bin/riot'
}
{'fraction_non_alphanumeric': 0.13499825358016068, 'fraction_numerical': 0.0017464198393293748, 'mean_word_length': 2.541743970315399, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1488200', 'n_tokens_mistral': 1783, 'n_tokens_neox': 1683, 'n_words': 372}