update-license.ts 4.4 KB
Newer Older
1 2
#!/usr/bin/env ts-node

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import {
  createReadStream,
  createWriteStream,
  promises as fsPromises,
  // link    as linkCallback,
  // unlink  as unlinkCallback,
}                            from 'fs'
import {
  Transform,
  // TransformOptions,
}                            from 'stream'
import { promisify }          from 'util'

import * as globCallback      from 'glob'

18
const LICENSE = `/**
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
19
 *   Wechaty Chatbot SDK - https://github.com/wechaty/wechaty
20
 *
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
21 22
 *   @copyright 2016 Huan LI (李卓桓) <https://github.com/huan>, and
 *                   Wechaty Contributors <https://github.com/wechaty>.
23 24 25 26 27 28 29 30 31 32 33 34 35 36
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */`
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
37

38
class LicenseTransformer extends Transform {
39

40 41 42 43 44 45
  private lineBuf = ''
  private lineNum = 0

  private updating  = false
  private updated   = false

46 47 48
  // constructor (options?: TransformOptions) {
  //   super(options)
  // }
49

50
  public _transform (chunk: any, _: string /* encoding: string */, done: () => void) {
51 52 53 54 55 56 57 58 59 60
    if (this.updated) {
      this.push(chunk)
    } else {
      const updatedChunk = this.updateChunk(chunk)
      this.push(updatedChunk)
    }

    done()
  }

61
  private updateChunk (chunk: any): string {
62 63 64 65 66 67 68 69 70 71 72
    const buffer  = this.lineBuf + chunk.toString()
    this.lineBuf    = ''

    if (!buffer) {
      console.error('no data')
      return ''
    }

    const updatedLineList: string[] = []

    buffer
73 74 75
      .split(/\n/)
      .forEach(line => {
        if (this.lineNum === 0 && line.startsWith('#!')) {
76
          updatedLineList.push(line)
77 78 79 80 81 82 83
        } else if (this.updated) {
          updatedLineList.push(line)
        } else if (this.updating) {
          if (/\*\//.test(line)) {
            updatedLineList.push(line.replace(/.*\*\//, LICENSE))
            this.updating = false
            this.updated  = true
84
          } else {
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
            // drop the old comments
          }
        } else {  // not updating and not updated. searching...
          if (!line) {
            updatedLineList.push(line)
          } else if (/\s*\/\*\*/.test(line)) {  // comment start
            if (/\*\//.test(line)) {  // comment end at the same line with start
              updatedLineList.push(line.replace(/\/\*\*.*\*\//, LICENSE))
              this.updated = true
            } else {
              this.updating = true
            }
          } else {  // not a comment. INSERT here
            updatedLineList.push(LICENSE)
            updatedLineList.push(line)
            this.updated = true
101 102 103
          }
        }

104 105 106
        this.lineBuf = line
        this.lineNum++
      })
107 108 109 110

    return updatedLineList.join('\n')
  }

111
  public _flush (done: () => void) {
112 113 114 115 116 117
    if (this.lineBuf) {
      this.push(this.lineBuf)
      this.lineBuf = ''
    }
    done()
  }
118

119 120
}

121
async function updateLicense (file: string): Promise<void> {
122 123 124 125 126
  const tmpFile = file + `.${process.pid}.tmp`
  const readStream  = createReadStream(file)
  const writeStream = createWriteStream(tmpFile)
  const tranStream  = new LicenseTransformer()

127
  console.info(`Updating LICENSE for file ${file}...`)
128 129 130 131 132
  await new Promise<void>((resolve, reject) => {
    readStream
      .pipe(tranStream)
      .pipe(writeStream)
      .on('close', resolve)
133
      .on('error', reject)
134
  })
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
135 136 137 138 139 140
  // await promisify(unlinkCallback)
  await fsPromises.unlink(file)
  // await promisify(linkCallback)(tmpFile, file)
  await fsPromises.link(tmpFile, file)
  // await promisify(unlinkCallback)(tmpFile)
  await fsPromises.unlink(tmpFile)
141 142
}

143
async function glob (pattern: string): Promise<string[]> {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
144
  return promisify<string, string[]>(globCallback as any)(pattern)
145
}
146

147
async function main (): Promise<number> {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
148
  const pattern = '{bin/**/*.ts,examples/**/*.{js,ts},scripts/**/*.{ts,js},src/**/*.{ts,js},tests/**/*.ts}'
149
  // const pattern = 't.ts'
150 151 152
  const srcFileList = await glob(pattern)
  const promiseList = srcFileList.map(updateLicense)
  await Promise.all(promiseList)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
153
  return 0
154 155 156
}

main()
157 158 159 160 161
  .then(process.exit)
  .catch(e => {
    console.error(e)
    process.exit(1)
  })