Gulp:建筑打字稿项目正在创建权限问题

时间:2019-04-08 16:20:25

标签: javascript node.js typescript gulp

我有一个打字稿项目,当我运行gulp构建时,我删除了目录中的文件夹,打字稿为我重新创建了目录(这是理想的结果)。

但是,似乎用奇怪的权限创建了它。当我执行ls -l时,我获得了用rimraf删除,然后使用打字稿创建的目录的以下权限。

$ ls -l
drwxrwxrwx 1 rnaddy rnaddy  512 Apr  1 10:48 src
d????????? ? ?      ?         ?            ? types

为什么显示问号?我觉得这会破坏我的构建,因为在构建时会出错,我认为这可能是问题所在。

$ gulp build:watch
[11:07:18] Using gulpfile ~\Documents\vscode\projects\red5\framework\gulpfile.js
[11:07:18] Starting 'build:watch'...
[11:07:18] Starting 'router'...
rimraf: EPERM: operation not permitted, unlink 'C:\Users\rnaddy\Documents\vscode\projects\red5\framework\router\types'
[11:07:20] 'router' errored after 1.99 s
[11:07:20] Error: EPERM: operation not permitted, mkdir 'C:\Users\rnaddy\Documents\vscode\projects\red5\framework\router\types'
[11:07:20] 'build:watch' errored after 2 s

在我的gulp文件中执行的函数如下所示:

const path = require('path')
const gulp = require('gulp')
const ts = require('gulp-typescript')
const sourcemaps = require('gulp-sourcemaps')
const rimraf = require('rimraf')

const projects = {
  router: './router',
  server: './server',
  storage: './storage',
  session: './session',
  template: './template',
  middleware: './middleware',
  // Optional dependencies
  mysql: './mysql'
}

const tasks = [
  'router', 'middleware', 'template', 'storage', 'session', 'server',
  // Optional dependencies
  'mysql'
]

/**
 * Builds the project
 * @param {string} projectRoot
 * @returns {Promise<void>}
 */
async function makeProject(projectRoot) {
  let err = await new Promise(resolve => rimraf(path.join(projectRoot, 'types'), (err) => resolve(err)))
  if (err) {
    console.error('rimraf:', err.message)
    // return resolve(false)
  }

  let tsResult = await new Promise(async resolve => {
    let tsProject = ts.createProject(path.join(projectRoot, 'tsconfig.json'))
    let tsResult = tsProject.src()
      .pipe(sourcemaps.init())
      .pipe(tsProject())
      .on('finish', () => resolve(tsResult))
  })

  return await new Promise(resolve => {
    tsResult.js
      .pipe(sourcemaps.write())
      .pipe(gulp.dest(path.join(projectRoot, 'lib')).on('end', () => {
        tsResult.dts.pipe(gulp.dest(path.join(projectRoot, 'types'))).on('finish', () => resolve())
        // gulp.src(path.join(projectRoot,'src/**/*.ts')).pipe(sourcemaps.init())
      }))
  })
}

gulp.task('router', () => makeProject(path.join(__dirname, projects.router)))
gulp.task('server', () => makeProject(path.join(__dirname, projects.server)))
gulp.task('storage', () => makeProject(path.join(__dirname, projects.storage)))
gulp.task('session', () => makeProject(path.join(__dirname, projects.session)))
gulp.task('template', () => makeProject(path.join(__dirname, projects.template)))
gulp.task('middleware', () => makeProject(path.join(__dirname, projects.middleware)))
// Optional dependencies
gulp.task('mysql', () => makeProject(path.join(__dirname, projects.mysql)))

gulp.task('build:watch', gulp.series([...tasks, () => {
  gulp.watch('./router/src/**/*.ts', gulp.series('router'))
  gulp.watch('./server/src/**/*.ts', gulp.series('server'))
  gulp.watch('./storage/src/**/*.ts', gulp.series('storage'))
  gulp.watch('./session/src/**/*.ts', gulp.series('session'))
  gulp.watch('./template/src/**/*.ts', gulp.series('template'))
  gulp.watch('./middleware/src/**/*.ts', gulp.series('middleware'))
  // Optional dependencies
  gulp.watch('./mysql/src/**', gulp.series('mysql'))
}]))

gulp.task('build', gulp.series(...tasks))

1 个答案:

答案 0 :(得分:0)

该问题似乎是由于rimraf引起的。我只需要删除目录的内容,而不是删除目录types

所以我只需要将types更改为types/*就可以了,就像这样:

rimraf(path.join(projectRoot, 'types/*'), (err) => /* do stuff*/)