Skip to content

Commit 0853d53

Browse files
committedMay 8, 2019
Optimize structure
1 parent a4f2d1e commit 0853d53

22 files changed

+188
-152
lines changed
 

‎.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.DS_Store
12
node_modules
2-
*.app
33
*.apk
4+
*.app
5+
*.ipa

‎.npmignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
.DS_Store
2-
.git*
32
.eslint*
43
.prettier*
54
*.apk

‎bin/cli.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
11
#!/usr/bin/env node
22

3-
require('../src/extract-pkg')
3+
// TODO:
4+
// - Support Windows OS for Android
5+
6+
const yargs = require('yargs')
7+
const { check, options } = require('./yargs')
8+
const logger = require('./utils/logger')
9+
const { version } = require('../package.json')
10+
11+
const isWin = process.platform === 'win32'
12+
13+
// no Windows support
14+
if (isWin) {
15+
logger.warn('No support Windows platform yet.')
16+
17+
process.exit(1)
18+
}
19+
20+
// prettier-ignore
21+
const argv = yargs
22+
.usage('Usage: $0 <ios|android> [options]')
23+
.help('help', 'show help').alias('help', 'h')
24+
.version('version', 'show version', `v${version}`).alias('version', 'V')
25+
.command('ios', 'extract .app from iOS simulator')
26+
.command('android', 'extract .apk from Android emulator')
27+
.demandCommand(1, 'choose a platform, <ios|android>')
28+
.config('config', '.js or JSON support', (c) => require(c)).alias('config', 'c')
29+
.options(options)
30+
.check(check)
31+
.argv
32+
33+
require('./main')(argv)

‎bin/constants.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict'
2+
3+
exports.IDS = ['id', 'to', 'rename']

‎bin/main.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict'
2+
3+
const { IDS } = require('./constants')
4+
const set = require('./utils/set')
5+
6+
function main (argv) {
7+
const { _ } = argv
8+
const [platform] = _
9+
10+
// filtered argv
11+
const nextArgv = Object.keys(argv).reduce((acc, key) => {
12+
if (IDS.includes(key)) {
13+
return {
14+
...acc,
15+
[key]: argv[key]
16+
}
17+
}
18+
19+
return acc
20+
}, {})
21+
22+
// set prioritization
23+
const args = set(IDS, [
24+
nextArgv,
25+
argv[platform] || {},
26+
{
27+
to: process.cwd()
28+
}
29+
])
30+
31+
const { extract, getFrom, cp } = require(`./platforms/${platform}`)
32+
33+
const from = getFrom(extract(args.id))
34+
const tmpArr = from.slice(0).split('/')
35+
const dirname = tmpArr.slice(0, -1).join('/')
36+
const [filename] = tmpArr.reverse()
37+
const prefix = args.rename ? 'Copy & Rename' : 'Copy'
38+
const dest = `${args.to}/${args.rename || filename}`
39+
40+
cp({ ...args, from, dest, prefix, dirname, filename })
41+
}
42+
43+
module.exports = main

‎bin/platforms/android.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict'
2+
3+
const command = require('../utils/command')
4+
5+
exports.extract = function extract (id) {
6+
const extract = command('Extract `.apk` from Android emulator')('adb')
7+
8+
extract(['shell', 'pm', 'path', id])
9+
}
10+
11+
exports.getFrom = function getFrom ({ stdout }) {
12+
return stdout.toString().replace(/package:|\n/g, '')
13+
}
14+
15+
exports.cp = function cp ({ from, to, dest, prefix, dirname, filename, rename }) {
16+
const cp = command(
17+
[
18+
`${prefix}:`,
19+
`From(emulator) - ${dirname}`,
20+
`To - ${to}`,
21+
`Filename - ${rename || filename}`
22+
].join('\n')
23+
)('adb')
24+
25+
cp(['pull', from, dest])
26+
}

‎bin/platforms/ios.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict'
2+
3+
const command = require('../utils/command')
4+
5+
exports.extract = function extract (id) {
6+
const extract = command('Extract `.app` from iOS simulator')('xcrun')
7+
8+
return extract(['simctl', 'get_app_container', 'booted', id])
9+
}
10+
11+
exports.getFrom = function getFrom ({ stdout }) {
12+
return stdout.split('\n').join('')
13+
}
14+
15+
exports.cp = function cp ({ from, to, dest, prefix, dirname, filename, rename }) {
16+
const cp = command(
17+
[
18+
`${prefix}:`,
19+
`From - ${dirname}`,
20+
`To - ${to}`,
21+
`Filename - ${rename || filename}`
22+
].join('\n')
23+
)('cp')
24+
25+
cp(['-r', from, dest])
26+
}

‎src/helpers/command.js ‎bin/utils/command.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict'
2+
13
const { spawnSync } = require('child_process')
24
const logger = require('./logger')
35

‎src/helpers/logger.js ‎bin/utils/logger.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict'
2+
13
const colors = require('colors') // eslint-disable-line
24

35
function info (prefix = '', message) {

‎bin/utils/set.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict'
2+
3+
function set (ids, args) {
4+
return args.reduce((acc, arg) => {
5+
const o = ids.reduce((nextArg, id) => {
6+
if (!acc[id] && arg[id]) {
7+
return {
8+
...nextArg,
9+
[id]: arg[id]
10+
}
11+
}
12+
13+
return nextArg
14+
}, {})
15+
16+
return {
17+
...o,
18+
...acc
19+
}
20+
}, {})
21+
}
22+
23+
module.exports = set

‎src/yargs/check.js ‎bin/yargs/check.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
module.exports = function check (argv) {
1+
'use strict'
2+
3+
function check (argv) {
24
const { _, id, ios = {}, android = {} } = argv
35
const [platform] = _
46
const config = platform === 'ios' ? ios : android
@@ -9,3 +11,5 @@ module.exports = function check (argv) {
911

1012
return true
1113
}
14+
15+
module.exports = check

‎src/yargs/index.js ‎bin/yargs/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict'
2+
13
const check = require('./check')
24
const options = require('./options')
35

‎src/yargs/options.js ‎bin/yargs/options.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict'
2+
13
module.exports = {
24
id: {
35
alias: 'i',

‎package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "extract-pkg",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "Extract .app, .apk from iOS simulator or Android emulator",
55
"main": "./bin/cli.js",
66
"scripts": {
@@ -22,6 +22,9 @@
2222
"app",
2323
"apk"
2424
],
25+
"files": [
26+
"bin/*"
27+
],
2528
"repository": {
2629
"type": "git",
2730
"url": "https://github.com/jsveron23/extract-pkg"

‎src/extract-pkg.js

-33
This file was deleted.

‎src/helpers/get.js

-13
This file was deleted.

‎src/helpers/set.js

-7
This file was deleted.

‎src/main.js

-9
This file was deleted.

‎src/platforms/android.js

-25
This file was deleted.

‎src/platforms/ios.js

-25
This file was deleted.

‎test/get.js

-31
This file was deleted.

‎test/set.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const expect = require('chai').expect
2-
const set = require('../src/helpers/set')
2+
const set = require('../bin/utils/set')
33

44
describe('set priorities of argv', () => {
55
const config = {
@@ -12,23 +12,35 @@ describe('set priorities of argv', () => {
1212
const id = 'com.arg.id'
1313
const to = '/arg/to'
1414
const rename = 'app-debug.app'
15-
const args = set({ id, to, rename, config })
15+
const args = set(['id', 'to', 'rename'], [{ id, to, rename }, config])
1616

1717
expect(args).to.have.property('id', id)
1818
expect(args).to.have.property('to', to)
1919
expect(args).to.have.property('rename', rename)
2020
})
2121

2222
it('use config props', () => {
23-
const args = set({ id: null, to: null, rename: null, config })
23+
const args = set(
24+
['id', 'to', 'rename'],
25+
[{ id: null, to: null, rename: null }, config]
26+
)
2427

2528
expect(args).to.have.property('id', config.id)
2629
expect(args).to.have.property('to', config.to)
2730
expect(args).to.have.property('rename', config.rename)
2831
})
2932

3033
it('`to` value assigned same as the command path', () => {
31-
const args = set({ id: null, to: null, config: {} })
34+
const args = set(
35+
['id', 'to', 'rename'],
36+
[
37+
{ id: null, to: null },
38+
{},
39+
{
40+
to: process.cwd()
41+
}
42+
]
43+
)
3244

3345
// TODO: help me by submitting PR to test process.cwd() properly
3446
expect(args).to.have.property('to', process.cwd())

0 commit comments

Comments
 (0)
Please sign in to comment.