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

replaces path to .asar.unpacked if cmd includes .asar/node_modules #155

Merged
merged 18 commits into from
Sep 4, 2017
Merged
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
23 changes: 23 additions & 0 deletions .appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
environment:
matrix:
- nodejs_version: "6"
- nodejs_version: "8"

# cache:
# - node_modules

platform:
- x64

install:
- ps: Install-Product node $env:nodejs_version $env:platform
- npm install

test_script:
- node --version
- npm --version
- npm test

build: off

version: "{build}"
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ If you need want to use an existing ipfs installation you can set `$IPFS_EXEC=/p

For more details see https://ipfs.github.io/js-ipfsd-ctl/.

### Packaging

`ipfsd-ctl` can be packaged in Electron applications, but the ipfs binary
has to be excluded from asar (Electron Archives),
[read more about unpack files from asar](https://electron.atom.io/docs/tutorial/application-packaging/#adding-unpacked-files-in-asar-archive).
`ipfsd-ctl` will try to detect if used from within an `app.asar` archive
and tries to resolve ipfs from `app.asar.unpacked`. The ipfs binary is part of
the `go-ipfs-dep` module.

```bash
electron-packager ./ --asar.unpackDir=node_modules/go-ipfs-dep
```

See [electron asar example](https://github.com/ipfs/js-ipfsd-ctl/tree/master/examples/electron-asar/)

## Contribute

Feel free to join in. All welcome. Open an [issue](https://github.com/ipfs/js-ipfsd-ctl/issues)!
Expand Down
1 change: 1 addition & 0 deletions examples/disposableApi.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint no-console: 0 */
'use strict'

// Start a disposable node, and get access to the api
Expand Down
2 changes: 2 additions & 0 deletions examples/electron-asar/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
test-ipfs-app*
34 changes: 34 additions & 0 deletions examples/electron-asar/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* eslint no-console: 0 */
'use strict'

const { app, ipcMain, BrowserWindow } = require('electron')
const ipfsd = require('ipfsd-ctl')

app.on('ready', () => {
const win = new BrowserWindow({
title: 'loading'
})
win.loadURL(`file://${app.getAppPath()}/public/index.html`)
})

ipcMain.on('start', ({ sender }) => {
console.log('starting disposable IPFS')
sender.send('message', 'starting disposable IPFS')

ipfsd.disposableApi((err, ipfs) => {
if (err) {
sender.send('error', err)
throw err
}
console.log('get id')
sender.send('message', 'get id')
ipfs.id(function (err, id) {
if (err) {
sender.send('error', err)
throw err
}
console.log('got id', id)
sender.send('id', JSON.stringify(id))
})
})
})
16 changes: 16 additions & 0 deletions examples/electron-asar/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "test-ipfs-app",
"private": true,
"main": "./app.js",
"dependencies": {
"ipfsd-ctl": "*"
},
"devDependencies": {
"electron": "^1.7.6",
"electron-packager": "^9.0.0"
},
"scripts": {
"start": "electron .",
"package": "electron-packager ./ --overwrite --asar.unpackDir=node_modules/go-ipfs-dep"
}
}
21 changes: 21 additions & 0 deletions examples/electron-asar/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!doctype html>
<title>loading</title>
<button>test ipfs app</button>
<script>
const { ipcRenderer } = require('electron')

ipcRenderer.on('message', (event, msg) => appendPre(msg))
ipcRenderer.on('error', (event, err) => appendPre(err))

document.querySelector('button')
.addEventListener('click', () => {
ipcRenderer.once('id', (event, id) => appendPre(id))
ipcRenderer.send('start')
})

function appendPre(data) {
const pre = document.createElement('pre')
pre.textContent = data
document.body.appendChild(pre)
}
</script>
6 changes: 6 additions & 0 deletions examples/electron-asar/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Packaging

```bash
npm install
npm run package
```
1 change: 1 addition & 0 deletions examples/id.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint no-console: 0 */
'use strict'

var ipfsd = require('../')
Expand Down
1 change: 1 addition & 0 deletions examples/local.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint no-console: 0 */
'use strict'

var ipfsd = require('../')
Expand Down
10 changes: 9 additions & 1 deletion src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ const GRACE_PERIOD = 7500 // amount of ms to wait before sigkill
function findIpfsExecutable () {
const rootPath = process.env.testpath ? process.env.testpath : __dirname

const appRoot = path.join(rootPath, '..')
let appRoot = path.join(rootPath, '..')
// If inside <appname>.asar try to load from .asar.unpacked
// this only works if asar was built with
// asar --unpack-dir=node_modules/go-ipfs-dep/* (not tested)
// or
// electron-packager ./ --asar.unpackDir=node_modules/go-ipfs-dep
if (appRoot.includes(`.asar${path.sep}`)) {
appRoot = appRoot.replace(`.asar${path.sep}`, `.asar.unpacked${path.sep}`)
}
Copy link
Member

Choose a reason for hiding this comment

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

Could you add this to the README too? It is fine to leave it here, but I want to make sure it this note is searchable and linkable to

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure

const depPath = path.join('go-ipfs-dep', 'go-ipfs', 'ipfs')
const npm3Path = path.join(appRoot, '../', depPath)
const npm2Path = path.join(appRoot, 'node_modules', depPath)
Expand Down
50 changes: 43 additions & 7 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const os = require('os')
const exec = require('../src/exec')
const ipfsd = require('../src')

const isWindows = os.platform() === 'win32'

describe('ipfs executable path', () => {
let Node

Expand All @@ -37,7 +39,7 @@ describe('ipfs executable path', () => {
Node = require('../src/node.js')
var node = new Node()
expect(node.exec)
.to.eql('/tmp/ipfsd-ctl-test/node_modules/go-ipfs-dep/go-ipfs/ipfs')
.to.eql(path.normalize('/tmp/ipfsd-ctl-test/node_modules/go-ipfs-dep/go-ipfs/ipfs'))
rimraf('/tmp/ipfsd-ctl-test', done)
})
})
Expand All @@ -58,7 +60,7 @@ describe('ipfs executable path', () => {
expect(
node.exec
).to.be.eql(
'/tmp/ipfsd-ctl-test/node_modules/ipfsd-ctl/node_modules/go-ipfs-dep/go-ipfs/ipfs'
path.normalize('/tmp/ipfsd-ctl-test/node_modules/ipfsd-ctl/node_modules/go-ipfs-dep/go-ipfs/ipfs')
)
rimraf('/tmp/ipfsd-ctl-test', done)
})
Expand Down Expand Up @@ -384,16 +386,46 @@ describe('daemons', () => {
})
})

// skip on windows for now
// https://github.com/ipfs/js-ipfsd-ctl/pull/155#issuecomment-326970190
// fails on windows see https://github.com/ipfs/js-ipfs-api/issues/408
if (isWindows) {
it.skip('uses the correct ipfs-api')
return // does not continue this test on win
}

// NOTE: if you change ./fixtures, the hash will need to be changed
it('uses the correct ipfs-api', (done) => {
ipfs.util.addFromFs(path.join(__dirname, 'fixtures/'), { recursive: true }, (err, res) => {
if (err) throw err
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As it is a temporary skip, is it ok if I just os.platform() === 'win32' ?

https://nodejs.org/api/os.html#os_os_platform

Copy link
Contributor Author

Choose a reason for hiding this comment

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


const added = res[res.length - 1]

// Temporary: Need to see what is going on on windows
expect(res).to.deep.equal([
{
path: 'fixtures/test.txt',
hash: 'Qmf412jQZiuVUtdgnB36FXFX7xg5V6KEbSJ4dpQuhkLyfD',
size: 19
},
{
path: 'fixtures',
hash: 'QmXkiTdnfRJjiQREtF5dWf2X4V9awNHQSn9YGofwVY4qUU',
size: 73
}
])

expect(res.length).to.equal(2)
expect(added).to.have.property('path', 'fixtures')
expect(added).to.have.property(
'hash',
'QmXkiTdnfRJjiQREtF5dWf2X4V9awNHQSn9YGofwVY4qUU'
)
expect(res[0]).to.have.property('path', 'fixtures/test.txt')
expect(res[0]).to.have.property(
'hash',
'Qmf412jQZiuVUtdgnB36FXFX7xg5V6KEbSJ4dpQuhkLyfD'
)
done()
})
})
Expand All @@ -404,11 +436,15 @@ describe('daemons', () => {
const dir = `${os.tmpdir()}/tmp-${Date.now() + '-' + Math.random().toString(36)}`

const check = (cb) => {
if (fs.existsSync(path.join(dir, 'repo.lock'))) {
cb(new Error('repo.lock not removed'))
}
if (fs.existsSync(path.join(dir, 'api'))) {
cb(new Error('api file not removed'))
// skip on windows
// https://github.com/ipfs/js-ipfsd-ctl/pull/155#issuecomment-326983530
if (!isWindows) {
if (fs.existsSync(path.join(dir, 'repo.lock'))) {
cb(new Error('repo.lock not removed'))
}
if (fs.existsSync(path.join(dir, 'api'))) {
cb(new Error('api file not removed'))
}
Copy link
Member

Choose a reason for hiding this comment

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

👍

}
cb()
}
Expand Down