{"action":"created","issue":{"url":"https://api.github.com/repos/ioquatix/shell-environment/issues/1","labels_url":"https://api.github.com/repos/ioquatix/shell-environment/issues/1/labels{/name}","comments_url":"https://api.github.com/repos/ioquatix/shell-environment/issues/1/comments","events_url":"https://api.github.com/repos/ioquatix/shell-environment/issues/1/events","html_url":"https://github.com/ioquatix/shell-environment/issues/1","id":72346780,"number":1,"title":"Prepare for 1.0.0 release","user":{"login":"ioquatix","id":30030,"avatar_url":"https://avatars.githubusercontent.com/u/30030?v=3","gravatar_id":"","url":"https://api.github.com/users/ioquatix","html_url":"https://github.com/ioquatix","followers_url":"https://api.github.com/users/ioquatix/followers","following_url":"https://api.github.com/users/ioquatix/following{/other_user}","gists_url":"https://api.github.com/users/ioquatix/gists{/gist_id}","starred_url":"https://api.github.com/users/ioquatix/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ioquatix/subscriptions","organizations_url":"https://api.github.com/users/ioquatix/orgs","repos_url":"https://api.github.com/users/ioquatix/repos","events_url":"https://api.github.com/users/ioquatix/events{/privacy}","received_events_url":"https://api.github.com/users/ioquatix/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-05-01T03:46:55Z","updated_at":"2015-05-01T04:10:16Z","closed_at":null,"body":"This code is a quick hack/extract of the existing code from script-runner.\r\n\r\nhttps://github.com/Glavin001/atom-beautify/issues/164#issuecomment-97903248\r\n\r\nI would suggest the only things that need to really be addressed:\r\n\r\n1. Naming and scope - is this the correct name and is this module covering the right set of functionality?\r\n2. Platform support - it is currently borked on Windows - should we simply return `platform.env` on windows?\r\n3. API - only one function.. should we provide some way to override the shell or `exports` function called? Perhaps an options hash?\r\n4. Testing - can we improve the testing? I've done some basic stuff.\r\n5. General code stuff - this is my first NPM module and I welcome feedback about it's structure.\r\n\r\n/cc @Glavin001 @huba\r\n"},"comment":{"url":"https://api.github.com/repos/ioquatix/shell-environment/issues/comments/98039328","html_url":"https://github.com/ioquatix/shell-environment/issues/1#issuecomment-98039328","issue_url":"https://api.github.com/repos/ioquatix/shell-environment/issues/1","id":98039328,"user":{"login":"Glavin001","id":1885333,"avatar_url":"https://avatars.githubusercontent.com/u/1885333?v=3","gravatar_id":"","url":"https://api.github.com/users/Glavin001","html_url":"https://github.com/Glavin001","followers_url":"https://api.github.com/users/Glavin001/followers","following_url":"https://api.github.com/users/Glavin001/following{/other_user}","gists_url":"https://api.github.com/users/Glavin001/gists{/gist_id}","starred_url":"https://api.github.com/users/Glavin001/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Glavin001/subscriptions","organizations_url":"https://api.github.com/users/Glavin001/orgs","repos_url":"https://api.github.com/users/Glavin001/repos","events_url":"https://api.github.com/users/Glavin001/events{/privacy}","received_events_url":"https://api.github.com/users/Glavin001/received_events","type":"User","site_admin":false},"created_at":"2015-05-01T04:10:15Z","updated_at":"2015-05-01T04:10:15Z","body":"My current version for the next release of Atom Beautify: https://github.com/Glavin001/atom-beautify/blob/240368fb09efaf67b31f10f0cf7f3fdba46fa65f/src/beautifiers/beautifier.coffee#L80-L131\r\n\r\n```coffee\r\n###\r\n    Get Shell Environment variables\r\n    Special thank you to @ioquatix\r\n    See https://github.com/ioquatix/script-runner/blob/v1.5.0/lib/script-runner.coffee#L45-L63\r\n    ###\r\n    _envCache: null\r\n    _envCacheDate: null\r\n    _envCacheExpiry: 10000 # 10 seconds\r\n    getShellEnvironment: ->\r\n        return new @Promise((resolve, reject) =>\r\n            # Check Cache\r\n            if @_envCache? and @_envCacheDate?\r\n                # Check if Cache is old\r\n                if (new Date() - @_envCacheDate) < @_envCacheExpiry\r\n                    # Still fresh\r\n                    return resolve(@_envCache)\r\n\r\n            # Check if Windows\r\n            isWin = /^win/.test(process.platform)\r\n            if isWin\r\n                # Windows\r\n                # Use default\r\n                resolve(process.env)\r\n            else\r\n                # Mac & Linux\r\n                # I tried using ChildProcess.execFile but there is no way to set detached and\r\n                # this causes the child shell to lock up.\r\n                # This command runs an interactive login shell and\r\n                # executes the export command to get a list of environment variables.\r\n                # We then use these to run the script:\r\n                child = spawn process.env.SHELL, ['-ilc', 'env'],\r\n                  # This is essential for interactive shells, otherwise it never finishes:\r\n                  detached: true,\r\n                  # We don't care about stdin, stderr can go out the usual way:\r\n                  stdio: ['ignore', 'pipe', process.stderr]\r\n                # We buffer stdout:\r\n                buffer = ''\r\n                child.stdout.on 'data', (data) -> buffer += data\r\n                # When the process finishes, extract the environment variables and pass them to the callback:\r\n                child.on 'close', (code, signal) =>\r\n                    if code isnt 0\r\n                        return reject(new Error(\"Could not get Shell Environment. Exit code: \"+code+\", Signal: \"+signal))\r\n                    environment = {}\r\n                    for definition in buffer.split('\\n')\r\n                        [key, value] = definition.split('=', 2)\r\n                        environment[key] = value if key != ''\r\n                    # Cache Environment\r\n                    @_envCache = environment\r\n                    @_envCacheDate = new Date()\r\n                    resolve(environment)\r\n          )\r\n```\r\n\r\nIt's [Promise-style using Bluebird](https://github.com/petkaantonov/bluebird).\r\n\r\nWe can convert it over to callback-style for `shell-environment` package.\r\n\r\nIt supports Window (falls back to `process.env`), Linux/Mac (your code), and caching (using `shell-environment` code about doubled my unit tests execution time for Atom Beautify, and I was able to cut it back in half to normal by caching the result)."}}