Posts tagged with “node”

Mocking Node.js Path Separators: The Dependency Injection Solution

The Problem

Node.js path utilities like path.sep, path.join() are hardcoded to the current platform and readonly - you can't mock them for cross-platform testing:

// Instead of this brittle approach:
function createTempFile(name: string, separator: string) {
  return 'tmp' + separator + name; // Manual string manipulation
}

// Use dependency injection:
function createTempFile(name: string, pathImpl = path) {
  return pathImpl.join('tmp', name);
}

// Now you can test both platforms reliably:
createTempFile('data.json', path.win32)  // → tmp\data.json
createTempFile('data.json', path.posix)  // → tmp/data.json

Why This Works

Node.js provides path.win32 and path.posix as separate implementations. Instead of fighting the platform dependency, embrace it through clean dependency injection. Test Windows logic on Linux, test Unix logic on Windows - no mocking needed.

How to use Jenkins to deploy a Node.JS backend project?

  1. Checkout code

  2. Set correct config file

  3. Scp to the remote machine

  4. Run deploy.sh on the remote machine by ssh

    • docker-compose build --no-cache
    • docker-compose down
    • docker-compose up -d

sshPublisher setup

  1. Manage Jenkins -> Config System
  • Find Publish over SSH
  • Add New Server, test connection
  1. Write directive in Jenkins file
            sshPublisher(
                continueOnError: false, failOnError: true,
                publishers: [
                 sshPublisherDesc(
                  configName: "yt-files-${BRANCH}",
                  verbose: true,
                  transfers: [
                   sshTransfer(
                    sourceFiles: "**/**",
                    remoteDirectory: "./publish/${BRANCH}",
                    removePrefix: '',
                    remoteDirectorySDF: false,
                    cleanRemote: true,
                    execCommand: "cd ./publish/${BRANCH} && bash deploy.sh ${BRANCH}"
                   )
                  ])
                ]
            )

要在Jenkins中新增对一个仓库的发布,需要以下步骤

  1. 创建流水线, 添加Git仓库,Credentials

  2. 为各分支添加必要的配置文件(不方便加入版本控制的机密信息)

  3. 写 Jenkinsfile