Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the dependency on npm APIs #123

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11,858 changes: 6,146 additions & 5,712 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 2 additions & 6 deletions src/apm-cli.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ path = require 'path'

_ = require 'underscore-plus'
colors = require 'colors'
npm = require 'npm'
yargs = require 'yargs'
wordwrap = require 'wordwrap'

Expand Down Expand Up @@ -144,11 +143,8 @@ getAtomVersion = (callback) ->
callback(unknownVersion)

getPythonVersion = (callback) ->
npmOptions =
userconfig: config.getUserConfigPath()
globalconfig: config.getGlobalConfigPath()
npm.load npmOptions, ->
python = npm.config.get('python') ? process.env.PYTHON
config.getSetting 'python', (python) ->
python ?= process.env.PYTHON
if config.isWin32() and not python
rootDir = process.env.SystemDrive ? 'C:\\'
rootDir += '\\' unless rootDir[rootDir.length - 1] is '\\'
Expand Down
19 changes: 11 additions & 8 deletions src/apm.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
child_process = require 'child_process'
fs = require './fs'
path = require 'path'
npm = require 'npm'
semver = require 'semver'
asarPath = null

Expand Down Expand Up @@ -108,14 +107,18 @@ module.exports =
else
fs.existsSync(path.join(@x86ProgramFilesDirectory(), "Microsoft Visual Studio", "#{version}", "BuildTools", "Common7", "IDE")) or fs.existsSync(path.join(@x86ProgramFilesDirectory(), "Microsoft Visual Studio", "#{version}", "Community", "Common7", "IDE")) or fs.existsSync(path.join(@x86ProgramFilesDirectory(), "Microsoft Visual Studio", "#{version}", "Enterprise", "Common7", "IDE")) or fs.existsSync(path.join(@x86ProgramFilesDirectory(), "Microsoft Visual Studio", "#{version}", "Professional", "Common7", "IDE")) or fs.existsSync(path.join(@x86ProgramFilesDirectory(), "Microsoft Visual Studio", "#{version}", "WDExpress", "Common7", "IDE"))

loadNpm: (callback) ->
npmOptions =
userconfig: @getUserConfigPath()
globalconfig: @getGlobalConfigPath()
npm.load npmOptions, -> callback(null, npm)

getSetting: (key, callback) ->
@loadNpm -> callback(npm.config.get(key))
args = [require.resolve('npm/bin/npm-cli'), '--globalconfig', @getGlobalConfigPath(), '--userconfig', @getUserConfigPath(), 'config', 'get', key]
spawned = child_process.spawn(process.execPath, args)
outputChunks = []
spawned.stderr.on 'data', (chunk) -> outputChunks.push(chunk)
spawned.stdout.on 'data', (chunk) -> outputChunks.push(chunk)
spawned.on 'error', ->
spawned.on 'close', (code) ->
if code is 0
[name, result] = Buffer.concat(outputChunks).toString().split(' ')
result = result?.trim()
callback(result)

setupApmRcFile: ->
try
Expand Down
9 changes: 4 additions & 5 deletions src/ci.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,18 @@ class Ci extends Command
fs.makeTreeSync(@atomDirectory)

env = _.extend({}, process.env, {HOME: @atomNodeDirectory, RUSTUP_HOME: config.getRustupHomeDirPath()})
@addBuildEnvVars(env)

installOptions = {env, streaming: options.argv.verbose}
@addBuildEnvVars env, (env) =>
installOptions = {env, streaming: options.argv.verbose}

@fork @atomNpmPath, installArgs, installOptions, (args...) =>
@logCommandResults(callback, args...)
@fork @atomNpmPath, installArgs, installOptions, (args...) =>
@logCommandResults(callback, args...)

run: (options) ->
{callback} = options
opts = @parseOptions(options.commandArgs)

commands = []
commands.push (callback) => config.loadNpm (error, @npm) => callback(error)
commands.push (cb) => @loadInstalledAtomMetadata(cb)
commands.push (cb) => @installModules(opts, cb)

Expand Down
49 changes: 27 additions & 22 deletions src/command.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,19 @@ class Command
else
config.getResourcePath (@resourcePath) => callback(@resourcePath)

addBuildEnvVars: (env) ->
addBuildEnvVars: (env, callback) ->
@updateWindowsEnv(env) if config.isWin32()
@addNodeBinToEnv(env)
@addProxyToEnv(env)

env.npm_config_runtime = "electron"
env.npm_config_target = @electronVersion
env.npm_config_disturl = config.getElectronUrl()
env.npm_config_arch = config.getElectronArch()
env.npm_config_target_arch = config.getElectronArch() # for node-pre-gyp

@addProxyToEnv env, (env) ->
callback(env)

getNpmBuildFlags: ->
["--target=#{@electronVersion}", "--disturl=#{config.getElectronUrl()}", "--arch=#{config.getElectronArch()}"]

Expand All @@ -128,23 +131,25 @@ class Command
else
env[pathKey]= nodeBinFolder

addProxyToEnv: (env) ->
httpProxy = @npm.config.get('proxy')
if httpProxy
env.HTTP_PROXY ?= httpProxy
env.http_proxy ?= httpProxy

httpsProxy = @npm.config.get('https-proxy')
if httpsProxy
env.HTTPS_PROXY ?= httpsProxy
env.https_proxy ?= httpsProxy

# node-gyp only checks HTTP_PROXY (as of node-gyp@4.0.0)
env.HTTP_PROXY ?= httpsProxy
env.http_proxy ?= httpsProxy

# node-gyp doesn't currently have an option for this so just set the
# environment variable to bypass strict SSL
# https://github.com/nodejs/node-gyp/issues/448
useStrictSsl = @npm.config.get('strict-ssl') ? true
env.NODE_TLS_REJECT_UNAUTHORIZED = 0 unless useStrictSsl
addProxyToEnv: (env, callback) ->
config.getSetting 'proxy', (httpProxy) ->
if httpProxy
env.HTTP_PROXY ?= httpProxy
env.http_proxy ?= httpProxy

config.getSetting 'https-proxy', (httpsProxy) ->
if httpsProxy
env.HTTPS_PROXY ?= httpsProxy
env.https_proxy ?= httpsProxy

# node-gyp only checks HTTP_PROXY (as of node-gyp@4.0.0)
env.HTTP_PROXY ?= httpsProxy
env.http_proxy ?= httpsProxy

# node-gyp doesn't currently have an option for this so just set the
# environment variable to bypass strict SSL
# https://github.com/nodejs/node-gyp/issues/448
config.getSetting 'strict-ssl', (useStrictSsl) ->
useStrictSsl ?= true
env.NODE_TLS_REJECT_UNAUTHORIZED = 0 unless useStrictSsl
callback(env)
9 changes: 4 additions & 5 deletions src/dedupe.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,11 @@ class Dedupe extends Command
fs.makeTreeSync(@atomDirectory)

env = _.extend({}, process.env, {HOME: @atomNodeDirectory, RUSTUP_HOME: config.getRustupHomeDirPath()})
@addBuildEnvVars(env)
@addBuildEnvVars env, (env) =>
dedupeOptions = {env}
dedupeOptions.cwd = options.cwd if options.cwd

dedupeOptions = {env}
dedupeOptions.cwd = options.cwd if options.cwd

@fork(@atomNpmPath, dedupeArgs, dedupeOptions, callback)
@fork(@atomNpmPath, dedupeArgs, dedupeOptions, callback)

createAtomDirectories: ->
fs.makeTreeSync(@atomDirectory)
Expand Down
8 changes: 2 additions & 6 deletions src/git.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{spawn} = require 'child_process'
path = require 'path'
_ = require 'underscore-plus'
npm = require 'npm'
config = require './apm'
fs = require './fs'

Expand Down Expand Up @@ -50,11 +49,8 @@ exports.addGitToEnv = (env) ->
addGitBashToEnv(env)

exports.getGitVersion = (callback) ->
npmOptions =
userconfig: config.getUserConfigPath()
globalconfig: config.getGlobalConfigPath()
npm.load npmOptions, ->
git = npm.config.get('git') ? 'git'
config.getSetting 'git', (git) ->
git ?= 'git'
exports.addGitToEnv(process.env)
spawned = spawn(git, ['--version'])
outputChunks = []
Expand Down
107 changes: 51 additions & 56 deletions src/install.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -74,47 +74,46 @@ class Install extends Command
fs.makeTreeSync(@atomDirectory)

env = _.extend({}, process.env, {HOME: @atomNodeDirectory, RUSTUP_HOME: config.getRustupHomeDirPath()})
@addBuildEnvVars(env)

installOptions = {env}
installOptions.streaming = true if @verbose

if installGlobally
installDirectory = temp.mkdirSync('apm-install-dir-')
nodeModulesDirectory = path.join(installDirectory, 'node_modules')
fs.makeTreeSync(nodeModulesDirectory)
installOptions.cwd = installDirectory

@fork @atomNpmPath, installArgs, installOptions, (code, stderr='', stdout='') =>
if code is 0
if installGlobally
commands = []
children = fs.readdirSync(nodeModulesDirectory)
.filter (dir) -> dir isnt ".bin"
assert.equal(children.length, 1, "Expected there to only be one child in node_modules")
child = children[0]
source = path.join(nodeModulesDirectory, child)
destination = path.join(@atomPackagesDirectory, child)
commands.push (next) -> fs.cp(source, destination, next)
commands.push (next) => @buildModuleCache(pack.name, next)
commands.push (next) => @warmCompileCache(pack.name, next)

async.waterfall commands, (error) =>
if error?
@logFailure()
else
@logSuccess() unless options.argv.json
callback(error, {name: child, installPath: destination})
@addBuildEnvVars env, (env) =>
installOptions = {env}
installOptions.streaming = true if @verbose

if installGlobally
installDirectory = temp.mkdirSync('apm-install-dir-')
nodeModulesDirectory = path.join(installDirectory, 'node_modules')
fs.makeTreeSync(nodeModulesDirectory)
installOptions.cwd = installDirectory

@fork @atomNpmPath, installArgs, installOptions, (code, stderr='', stdout='') =>
if code is 0
if installGlobally
commands = []
children = fs.readdirSync(nodeModulesDirectory)
.filter (dir) -> dir isnt ".bin"
assert.equal(children.length, 1, "Expected there to only be one child in node_modules")
child = children[0]
source = path.join(nodeModulesDirectory, child)
destination = path.join(@atomPackagesDirectory, child)
commands.push (next) -> fs.cp(source, destination, next)
commands.push (next) => @buildModuleCache(pack.name, next)
commands.push (next) => @warmCompileCache(pack.name, next)

async.waterfall commands, (error) =>
if error?
@logFailure()
else
@logSuccess() unless options.argv.json
callback(error, {name: child, installPath: destination})
else
callback(null, {name: child, installPath: destination})
else
callback(null, {name: child, installPath: destination})
else
if installGlobally
fs.removeSync(installDirectory)
@logFailure()
if installGlobally
fs.removeSync(installDirectory)
@logFailure()

error = "#{stdout}\n#{stderr}"
error = @getGitErrorMessage(pack) if error.indexOf('code ENOGIT') isnt -1
callback(error)
error = "#{stdout}\n#{stderr}"
error = @getGitErrorMessage(pack) if error.indexOf('code ENOGIT') isnt -1
callback(error)

getGitErrorMessage: (pack) ->
message = """
Expand Down Expand Up @@ -166,13 +165,12 @@ class Install extends Command
fs.makeTreeSync(@atomDirectory)

env = _.extend({}, process.env, {HOME: @atomNodeDirectory, RUSTUP_HOME: config.getRustupHomeDirPath()})
@addBuildEnvVars(env)
@addBuildEnvVars env, (env) =>
installOptions = {env}
installOptions.cwd = options.cwd if options.cwd
installOptions.streaming = true if @verbose

installOptions = {env}
installOptions.cwd = options.cwd if options.cwd
installOptions.streaming = true if @verbose

@fork(@atomNpmPath, installArgs, installOptions, callback)
@fork(@atomNpmPath, installArgs, installOptions, callback)

# Request package information from the atom.io API for a given package name.
#
Expand Down Expand Up @@ -376,15 +374,14 @@ class Install extends Command
fs.makeTreeSync(@atomDirectory)

env = _.extend({}, process.env, {HOME: @atomNodeDirectory, RUSTUP_HOME: config.getRustupHomeDirPath()})
@addBuildEnvVars(env)

buildOptions = {env}
buildOptions.streaming = true if @verbose
@addBuildEnvVars env, (env) =>
buildOptions = {env}
buildOptions.streaming = true if @verbose

fs.removeSync(path.resolve(__dirname, '..', 'native-module', 'build'))
fs.removeSync(path.resolve(__dirname, '..', 'native-module', 'build'))

@fork @atomNpmPath, buildArgs, buildOptions, (args...) =>
@logCommandResults(callback, args...)
@fork @atomNpmPath, buildArgs, buildOptions, (args...) =>
@logCommandResults(callback, args...)

packageNamesFromPath: (filePath) ->
filePath = path.resolve(filePath)
Expand Down Expand Up @@ -558,9 +555,8 @@ class Install extends Command
@createAtomDirectories()

if options.argv.check
config.loadNpm (error, @npm) =>
@loadInstalledAtomMetadata =>
@checkNativeBuildTools(callback)
@loadInstalledAtomMetadata =>
@checkNativeBuildTools(callback)
return

@verbose = options.argv.verbose
Expand Down Expand Up @@ -600,7 +596,6 @@ class Install extends Command
packageNames.push('.') if packageNames.length is 0

commands = []
commands.push (callback) => config.loadNpm (error, @npm) => callback(error)
commands.push (callback) => @loadInstalledAtomMetadata -> callback()
packageNames.forEach (packageName) ->
commands.push (callback) -> installPackage(packageName, callback)
Expand Down
22 changes: 10 additions & 12 deletions src/rebuild.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,18 @@ class Rebuild extends Command
fs.makeTreeSync(@atomDirectory)

env = _.extend({}, process.env, {HOME: @atomNodeDirectory, RUSTUP_HOME: config.getRustupHomeDirPath()})
@addBuildEnvVars(env)

@fork(@atomNpmPath, rebuildArgs, {env}, callback)
@addBuildEnvVars env, (env) =>
@fork(@atomNpmPath, rebuildArgs, {env}, callback)

run: (options) ->
{callback} = options
options = @parseOptions(options.commandArgs)

config.loadNpm (error, @npm) =>
@loadInstalledAtomMetadata =>
@forkNpmRebuild options, (code, stderr='') =>
if code is 0
@logSuccess()
callback()
else
@logFailure()
callback(stderr)
@loadInstalledAtomMetadata =>
@forkNpmRebuild options, (code, stderr='') =>
if code is 0
@logSuccess()
callback()
else
@logFailure()
callback(stderr)
26 changes: 11 additions & 15 deletions src/request.coffee
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
npm = require 'npm'
request = require 'request'

config = require './apm'

loadNpm = (callback) ->
npmOptions =
userconfig: config.getUserConfigPath()
globalconfig: config.getGlobalConfigPath()
npm.load(npmOptions, callback)

configureRequest = (requestOptions, callback) ->
loadNpm ->
requestOptions.proxy ?= npm.config.get('https-proxy') or npm.config.get('proxy') or process.env.HTTPS_PROXY or process.env.HTTP_PROXY
requestOptions.strictSSL ?= npm.config.get('strict-ssl')

userAgent = npm.config.get('user-agent') ? "AtomApm/#{require('../package.json').version}"
requestOptions.headers ?= {}
requestOptions.headers['User-Agent'] ?= userAgent
callback()
config.getSetting 'proxy', (proxy) ->
Copy link

@icecream17 icecream17 Jan 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edit: Invalid comment because the only way to not return is error or infinite wait, stopping execution

config.getSetting 'https-proxy', (httpsProxy) ->
config.getSetting 'strict-ssl', (strictSsl) ->
config.getSetting 'user-agent', (userAgent) ->
requestOptions.proxy ?= httpsProxy or proxy or process.env.HTTPS_PROXY or process.env.HTTP_PROXY
requestOptions.strictSSL ?= strictSsl

userAgent = userAgent ? "AtomApm/#{require('../package.json').version}"
requestOptions.headers ?= {}
requestOptions.headers['User-Agent'] ?= userAgent
callback()

module.exports =
get: (requestOptions, callback) ->
Expand Down