2017/06/28

Monitor File Change And Execute Code

็พๅœจๅฏซ PHP ็š„ๆ™‚ๅ€™้ƒฝๆœƒ่ฉฆ่‘—ๅœจ node.js ไปฅๅŠ shell script ไธŠๅฏฆ็พ,ๆ‰€ไปฅๆœƒไธๆ–ท็š„ๅ˜—่ฉฆ็จ‹ๅผ็ขผ,ๆฏๆฌก่ฆ็œ‹ๅŸท่กŒ็ตๆžœ้ƒฝ่ฆไธ‹ไธ€ๆฌก command ไนŸๆŒบ็ดฏ็š„,ๅฏซไบ†ไธ€้šป w.js ไพ†็›ฃๆŽง้€™ไธ‰็จฎๆช”ๆกˆ็š„่ฎŠๅ‹•ไธฆๅธถๅ…ฅๅฐๆ‡‰็š„ๅ‘ผๅซๆ–นๅผไพ†็ฏ€็œๆ™‚้–“

ๆญคๅŠŸ่ƒฝ้œ€ๅฎ‰่ฃ watch,ๅŸท่กŒๆ–นๆณ•็‚บ node w.js xxx.php

w.js
const watch = require('watch');
const { exec } = require('child_process');
const path = require('path');
const argvs = process.argv;
const target = argvs[2];
const fs = require('fs');
const commandMap = {
    sh: 'bash',
    php: 'php',
    js: 'node'
};

if (target === undefined) {
    console.log('Need file argument!');
    return;
}

if (!fs.existsSync(target)) {
    console.log(`${target} is not exists!`);
    return;
}

let ext = path.extname(target).replace(/\./g, '');

if (Object.keys(commandMap).indexOf(ext) === -1) {
    console.log(`${ext} is not legal!`);
    return;
}

console.log(`Start to watch "${target}"!`);

watch.createMonitor('./', (monitor) => {
    monitor.on('changed', (file) => {
        if (file === target) {
            exec(`clear; ${commandMap[ext]} ${file}`, (error, stdout, stderr) => {
                if (error) {
                    console.error(`exec error: ${error}`);
                    return;
                }

                console.log(stdout);
                console.log(stderr);
            });
        }
    });
});

ๅฏฆ้š›ไฝฟ็”จ็•ซ้ขๅคงๆฆ‚ๆ˜ฏ้€™ๆจฃ

w.js ็คบๆ„ๅœ–

2017/06/27

Random String

ๅ…ฌๅธๆœ‰ไธ€ๅ€‹้œ€ๆฑ‚ๆƒณ่ฆ็”ข็”Ÿ unique id,้›–็„ถ PHP ๆœ‰ uniqid ๅฏไปฅไฝฟ็”จ,ไฝ†ไป–ๆœ€ๅŸบๆœฌ็š„้•ทๅบฆๆ˜ฏ 13 ๅ€‹ๅญ—ๅ…ƒ,ๆ‰€ไปฅๆฑบๅฎš่‡ชๅทฑๅˆปไธ€ๅ€‹ๅˆฉ็”จ 0-9、a-z ็”ข็”Ÿ็š„ไบ‚ๆ•ธๅฏ†็ขผ,ๆœƒๆœ‰ PHP、nodejs ไปฅๅŠ shell script ็‰ˆๆœฌ。

PHP
<?php

function code($length = 10)
{
    $codeString = '0123456789abcdefghijklmnopqrstubwxyz';
    $codeStringSplit = str_split($codeString);
    $codeStringSize = count($codeStringSplit);
    $code = '';

    for ($i = 0; $i < $length; ++$i) {
        $rand = mt_rand(0, $codeStringSize - 1);
        $code .= $codeStringSplit[$rand];
    }

    return $code;
}

for ($i = 0; $i < 10; $i++) {
    echo code().PHP_EOL;
}
node.js
function code(length) {
    var stringLength = length || 10;
    var codeString = '0123456789abcdefghijklmnopqrstubwxyz';
    var codeStringSplit = codeString.split('');
    var codeStringSize = codeStringSplit.length;
    var code = '';
    var min = 0;
    var max = codeStringSize - 1;

    for (var i = 0; i < stringLength; ++i) {
        var rand = Math.floor(Math.random() * (max - min + 1)) + min;

        code += codeStringSplit[rand];
    }

    return code;
}

for (var i = 0; i < 10; ++i) {
    console.log(code());
}
bash
#!/bin/bash

function code() {
    result=""
    length=10
    codeString="0123456789abcdefghijklmnopqrstubwxyz";
    declare -a array

    for (( i = 0; i < ${#codeString}; i++ )); do
        array+=(${codeString:$i:1})
    done

    if [[ -n $1 ]]; then
        declare -i length=$1
    fi

    max=$((${#array[@]} - 1))
    
    for (( i = 0; i < ${length}; i++ )); do
        rand=$(shuf -i 0-${max} -n 1)
        result="${result}${array[${rand}]}"
    done

    echo ${result}
}

for (( j = 0; j < 10; j++ )); do
    code
done