Search code examples
javascriptnode.jstypescriptgulp

Gulp: Building typescript project is creating permission issues


I have a typescript project, and when I run my gulp build I remove a folder in a directory and typescript remakes the directory for me (Which is the desired result).

However, It seems to create it with strange permissions. When I do an ls -l, I get the following permissions on the directory that was deleted with rimraf, and then created with typescript.

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

Why is it showing the question marks? I feel that this is breaking my build, because when I build I get errors, and I think this might be the issue.

$ 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

The function that gets executed in my gulp file looks like this:

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))

Solution

  • It looks like the issue was because of rimraf. Instead of deleting the directory types, I just needed to delete the contents of the directory.

    So I just needed to change types to types/* so it looks like this:

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