Build can now use configurable path to node_modules
[redakcja.git] / apps / build / management / commands / build.py
1 import os
2 from subprocess import call
3 from optparse import make_option
4
5 from django.core.management.base import BaseCommand, CommandError
6 from django.core.management import call_command
7
8
9 class Command(BaseCommand):
10     
11     option_list = BaseCommand.option_list + (
12         make_option('--node-bin-path',
13             action='store',
14             dest='node_bin_path',
15             type='string',
16             default=None,
17             help='Path to node binary'),
18         make_option('--npm-bin',
19             action='store',
20             dest='npm_bin',
21             type='string',
22             default='npm',
23             help='Path to npm binary'),
24         make_option('--editor-npm-env',
25             action='store',
26             dest='editor_npm_env',
27             type='string',
28             default=None,
29             help='Destination path of npm environment, defaults to ./node_modules'),
30         )
31
32     def handle(self, **options):
33         wiki_base_dir = os.path.join(os.getcwd(), 'apps', 'wiki', 'static', 'wiki')
34         rng_base_dir = os.path.join(wiki_base_dir, 'editor')
35         build_dir = os.path.join(wiki_base_dir, 'build')
36
37         self.stdout.write('Installing editor dependencies')
38         if options['editor_npm_env']:
39             npm_env = os.path.join(rng_base_dir, options['editor_npm_env'])
40             if not os.path.exists(npm_env):
41                 os.makedirs(npm_env)
42             assert os.path.isdir(npm_env)
43             os.symlink(npm_env, os.path.join(rng_base_dir, 'node_modules'))
44         try:
45             call([options['npm_bin'], 'install'], cwd = rng_base_dir)
46         except OSError:
47             raise CommandError('Something went wrong, propably npm binary not found. Tried: %s' % options['npm_bin'])
48
49         self.stdout.write('Building editor')
50         if options['node_bin_path']:
51             # grunt needs npm binary to be foundable in PATH
52             os.environ['PATH'] = '%s:%s' % (options['node_bin_path'], os.environ['PATH'])
53         call(['./node_modules/.bin/grunt', 'build', '--output-dir=%s' % build_dir], cwd = rng_base_dir)
54
55         call_command('collectstatic', interactive = False, ignore_patterns = ['editor'])